4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * snmp/lustre-snmp-util.c
36 * Author: PJ Kirner <pjkirner@clusterfs.com>
40 * include important headers
43 #include <net-snmp/net-snmp-config.h>
44 #include <net-snmp/net-snmp-includes.h>
45 #include <net-snmp/agent/net-snmp-agent-includes.h>
51 #include <sys/types.h>
52 #if defined (__linux__)
61 #include "lustre-snmp-util.h"
63 /*********************************************************************
64 * Function: get_file_list
66 * Description: For the given valid directory path, returns the list
67 * all directories or files in that path.
69 * Input: 'dirname' the directory path.
70 * 'file_type' if this takes the value DIR_TYPE then
71 * returns the list of directories in that path.
72 * If its of type FILE_TYPE then returns the list of files
74 * 'count' pointer to number of elements returned in the
77 * Output: List of directories/files in that path.
79 *********************************************************************/
81 char *get_file_list(const char *dirname, int file_type, uint32_t *count)
85 struct dirent *pdirent = NULL;
90 char filename[MAX_PATH_SIZE];
93 if ((dirname == NULL) || ((pdir = opendir(dirname)) == NULL )) {
94 if (dirname == NULL) {
95 report("%s %s:line %d %s", __FILE__, __FUNCTION__, __LINE__,
96 "NULL directory is passed as parameter to funtion");
98 report("%s %s:line %d Error in opening the dir %s", __FILE__,
99 __FUNCTION__, __LINE__, dirname);
107 if ((pdirent = readdir(pdir)) == NULL)
110 /* Skip over '.' and '..' directores */
111 if ((pdirent->d_name[0] == '.') ||
112 !strcmp(pdirent->d_name, FILENAME_NUM_REF))
115 sprintf(filename, "%s/%s", dirname, pdirent->d_name);
116 cond1 = (file_type == FILE_TYPE) && is_directory(filename);
117 cond2 = (file_type == DIR_TYPE) && (!is_directory(filename));
122 /* Calculate the number of bytes for this new entry.*/
123 byte_count += strlen(pdirent->d_name) + 1;
129 if (file_count != 0) {
131 /* need one extra one for the finall NULL terminator*/
132 if ((ret_str = (char *) malloc(byte_count + 1)) == NULL) {
133 report("get_file_list() failed to malloc(%d)",byte_count+1);
140 while (file_count != 0) {
141 if ((pdirent = readdir(pdir)) == NULL)
144 if ((pdirent->d_name[0] == '.') ||
145 !strcmp(pdirent->d_name, FILENAME_NUM_REF))
148 sprintf(filename, "%s/%s", dirname, pdirent->d_name);
149 cond1 = (file_type == FILE_TYPE) && is_directory(filename);
150 cond2 = (file_type == DIR_TYPE) && (!is_directory(filename));
155 strcpy(ret_str + curr_offset, pdirent->d_name);
156 curr_offset = curr_offset + strlen(pdirent->d_name) + 1;
159 /* Put in the finall null terminator*/
160 ret_str[byte_count] = '\0';
167 /*********************************************************************
168 * Function: is_directory
170 * Description: Checks if given filename is a directory or not.
171 * all directories or files in that path.
173 * Input: 'filename' the directory path to be checked.
175 * Output: Returns 1 if its a directory else 0.
177 *********************************************************************/
179 int is_directory(const char *filename)
185 result = stat(filename, &statf);
186 return ((result == SUCCESS) && (statf.st_mode & S_IFDIR));
189 /*********************************************************************
190 * Function: read_string
192 * Description: For the given valid file path, reads the data in
195 * Input: 'filepath' the file whose data is to be accessed.
196 * 'lustre_var' the data from the file is read into
197 * this variable, returned to the requestor.
198 * 'var_max_size' the max size of the string
199 * 'report_error' boolean if error should be reported on
202 * Output: Returns SUCCESS if read successfully from file else
204 *********************************************************************/
206 int read_string(const char *filepath, char *lustre_var, size_t var_max_size)
210 int ret_val = SUCCESS;
211 int report_error = 1;
213 if ((filepath == NULL) || (lustre_var == NULL)) {
214 report("%s %s:line %d %s", __FILE__, __FUNCTION__, __LINE__,
215 "Input parameter is NULL");
218 fptr = fopen(filepath, "r");
222 report("%s %s:line %d Unable to open the file %s", __FILE__,
223 __FUNCTION__, __LINE__, filepath);
226 if (fgets(lustre_var, var_max_size, fptr) == NULL) {
227 report("%s %s:line %d read failed for file %s", __FILE__,
228 __FUNCTION__, __LINE__, filepath);
231 len = strlen(lustre_var);
233 Last char is EOF, before string ends,
234 so '\0' is moved to last but one.
236 lustre_var[len-1] = lustre_var[len];
244 /**************************************************************************
245 * Function: lustrefs_ctrl
247 * Description: Execute /etc/init.d/lustre script for starting,
248 * stopping and restarting Lustre services in child process.
250 * Input: Start/Stop/Restart Command Number.
251 * Output: Returns void
253 **************************************************************************/
255 void lustrefs_ctrl(int command)
259 cmd[0] = LUSTRE_SERVICE;
278 report("failed to execvp(\'%s %s\')",cmd[0],cmd[1]);
283 /*****************************************************************************
284 * Function: get_sysstatus
286 * Description: Read /var/lustre/sysStatus file, and based on file contents
287 * return the status of Lustre services.
290 * Output: Return ONLINE/OFFLINE/ONLINE PENDING/OFFLINE PENDING status
293 ****************************************************************************/
295 int get_sysstatus(void)
297 int ret_val = ERROR ;
298 char sys_status[50] = {0};
300 if(SUCCESS == read_string(FILENAME_SYS_STATUS,sys_status,sizeof(sys_status)))
302 if (memcmp(sys_status, STR_ONLINE_PENDING,strlen(STR_ONLINE_PENDING)) == 0)
303 ret_val = ONLINE_PENDING;
304 else if (memcmp(sys_status, STR_ONLINE, strlen(STR_ONLINE)) == 0)
306 else if (memcmp(sys_status, STR_OFFLINE_PENDING,strlen(STR_OFFLINE_PENDING)) == 0)
307 ret_val = OFFLINE_PENDING;
308 else if (memcmp(sys_status, STR_OFFLINE, strlen(STR_OFFLINE)) == 0)
311 report("%s %s:line %d Bad Contents in file %s \'%s\'", __FILE__,
312 __FUNCTION__, __LINE__, FILENAME_SYS_STATUS,sys_status);
318 /*****************************************************************************
319 * Function: read_ulong
321 * Description: Read long values from lproc and copy to the location
322 * pointed by input parameter.
324 * Input: file path, and pointer for data to be copied
326 * Output: Return ERROR or SUCCESS.
328 ****************************************************************************/
330 int read_ulong(const char *file_path, unsigned long *valuep)
332 char file_data[MAX_LINE_SIZE];
335 if ((ret_val = read_string(file_path, file_data,sizeof(file_data))) == SUCCESS){
336 *valuep = strtoul(file_data,NULL,10);
341 /*****************************************************************************
342 * Function: read_counter64
344 * Description: Read counter64 values from lproc and copy to the location
345 * pointed by input parameter.
347 * Input: file path, and pointer for data to be copied
349 * Output: Return ERROR or SUCCESS.
351 ****************************************************************************/
353 int read_counter64(const char *file_path, counter64 *c64,int factor)
355 char file_data[MAX_LINE_SIZE];
357 unsigned long long tmp = 0;
359 if ((ret_val = read_string(file_path, file_data,sizeof(file_data))) == SUCCESS) {
360 tmp = atoll(file_data) * factor;
361 c64->low = (unsigned long) (0x0FFFFFFFF & tmp);
362 tmp >>= 32; /* Shift right by 4 bytes */
363 c64->high = (unsigned long) (0x0FFFFFFFF & tmp);
368 /*****************************************************************************
369 * Function: get_nth_entry_from_list
371 * Description: Find the n'th entry from a null terminated list of string
373 * Input: dir_list - the list
374 * num - the number of elements in the list
375 * index - the index we are looking for
377 * Output: Return NULL on failure, or the string name on success.
379 ****************************************************************************/
381 const char *get_nth_entry_from_list(const char* dir_list,int num,int index)
388 * if we've reached the end of the list for some reason
389 * because num was wrong then stop processing
391 if( *(dir_list+cur_ptr) == 0)
394 /* If we've found the right one */
396 return dir_list+cur_ptr;
398 /* Move to the next one*/
399 cur_ptr += strlen(dir_list + cur_ptr)+1;
404 /*****************************************************************************
407 * Description: This function used to report error msg to stderr and log into
408 * log file(default file:/var/log/snmpd.log) when agent is started with
409 * debug option -Dlsnmpd
410 * Input: format string and variable arguments.
412 ****************************************************************************/
414 void report(const char *fmt, ...)
419 va_start(arg_list, fmt);
420 vsprintf(buf, fmt, arg_list);
423 DEBUGMSGTL(("lsnmpd", "%s\n", buf));
424 fprintf(stderr, "%s\n", buf);
430 /**************************************************************************
431 * Function: oid_table_ulong_handler
433 * Description: Fetch a unsigned long from the given location.
434 * Setup var_len, and return a pointer to the data.
436 * Input: file_path, and var_len pointer
438 * Output: NULL on failure, or pointer to data
440 **************************************************************************/
443 oid_table_ulong_handler(
444 const char* file_path,
447 static unsigned long ulong_ret;
448 if (SUCCESS != read_ulong(file_path,&ulong_ret))
450 *var_len = sizeof(ulong_ret);
451 return (unsigned char *) &ulong_ret;
454 /**************************************************************************
455 * Function: oid_table_c64_handler
457 * Description: Fetch a counter64 from the given location.
458 * Setup var_len, and return a pointer to the data.
460 * Input: file_path, and var_len pointer
462 * Output: NULL on failure, or pointer to data
464 **************************************************************************/
466 unsigned char* oid_table_c64_handler(const char* file_path,size_t *var_len)
468 static counter64 c64;
469 if (SUCCESS != read_counter64(file_path,&c64,1))
471 *var_len = sizeof(c64);
472 return (unsigned char *) &c64;
475 /**************************************************************************
476 * Function: oid_table_c64_kb_handler
478 * Description: Fetch a counter64 from the given location.
479 * Setup var_len, and return a pointer to the data.
480 * Different than oid_table_c64_handler in that
481 * the original value is multiplied by 1024 before converting
482 * to a counter64. (e.g. turn KB into a Byte scaled value)
484 * Input: file_path, and var_len pointer
486 * Output: NULL on failure, or pointer to data
488 **************************************************************************/
490 unsigned char* oid_table_c64_kb_handler(const char* file_path,size_t *var_len)
492 static counter64 c64;
493 /* scale by factor of 1024*/
494 if (SUCCESS != read_counter64(file_path,&c64,1024))
496 *var_len = sizeof(c64);
497 return (unsigned char *) &c64;
500 /**************************************************************************
501 * Function: oid_table_obj_name_handler
503 * Description: Just copy the file_path and return as the output value.
505 * Input: file_path, and var_len pointer
507 * Output: NULL on failure, or pointer to data
509 **************************************************************************/
512 oid_table_obj_name_handler(
513 const char* file_path,
516 static unsigned char string[SPRINT_MAX_LEN];
517 *var_len = strlen(file_path);
518 *var_len = MIN_LEN(*var_len, sizeof(string));
519 memcpy(string, file_path, *var_len);
520 return (unsigned char *) string;
523 /**************************************************************************
524 * Function: oid_table_string_handler
526 * Description: Fetch a string from the given location.
527 * Setup var_len, and return a pointer to the data.
529 * Input: file_path, and var_len pointer
531 * Output: NULL on failure, or pointer to data
533 **************************************************************************/
536 oid_table_string_handler(
537 const char* file_path,
540 static unsigned char string[SPRINT_MAX_LEN];
541 if( SUCCESS != read_string(file_path, (char *)string,sizeof(string)))
543 *var_len = strlen((char *)string);
544 return (unsigned char *) string;
548 /**************************************************************************
549 * Function: oid_table_is_directory_handler
551 * Description: Determine if the file_path is a directory.
552 * Setup a boolean return value.
553 * Setup var_len, and return a pointer to the data.
555 * Input: file_path, and var_len pointer
557 * Output: NULL on failure, or pointer to data
559 **************************************************************************/
562 oid_table_is_directory_handler(
563 const char* file_path,
566 static long long_ret;
567 long_ret = is_directory(file_path);
568 *var_len = sizeof(long_ret);
569 return (unsigned char *) &long_ret;
572 /**************************************************************************
573 * Function: var_genericTable
575 * Description: Handle Table driven OID processing
577 **************************************************************************/
580 var_genericTable(struct variable *vp,
585 WriteMethod **write_method,
587 struct oid_table *ptable)
592 unsigned char *ret_val = NULL;
594 const char* obj_name;
598 * Get the list of file. If there are no elements
601 if( 0 == (dir_list = get_file_list(path, DIR_TYPE, &num)))
607 if (header_simple_table(vp,name,length,exact,var_len,write_method, num)
609 goto cleanup_and_exit;
612 * The number of the device we're looking at
614 deviceindex = name[*length - 1] - 1;
617 * If we couldn't find this element
618 * something must have recently changed return
621 if(deviceindex >= num){
622 report("deviceindex=%d exceeds number of elements=%d",deviceindex,num);
623 goto cleanup_and_exit;
627 * Fetch the object name from the list
629 obj_name = get_nth_entry_from_list(dir_list,num,deviceindex);
630 if(obj_name == NULL){
632 * Note this should never really happen because we check deviceindex >=num
633 * above. And dir_list should be consitent with num
634 * but just in case...
636 report("object name not found in list",deviceindex,num);
637 goto cleanup_and_exit;
641 * Find the matching magic - or the end of the list
643 while(ptable[i].magic != vp->magic && ptable[i].magic != 0)
647 * If we didn't find a matching entry return
649 if(ptable[i].magic==0)
650 goto cleanup_and_exit;
653 * If the name is NULL is a special case and
654 * just just pass the obj_name as the file_path
655 * otherwise we create a file path from the given components
657 if(ptable[i].name != 0){
658 char file_path[MAX_PATH_SIZE];
659 sprintf(file_path, "%s%s/%s",path,obj_name,ptable[i].name);
660 ret_val = ptable[i].fhandler(file_path,var_len);
663 ret_val = ptable[i].fhandler(obj_name,var_len);
670 /**************************************************************************
671 * Function: stats_values
673 * Description: Setup nb_sample, min, max, sum and sum_square stats values
674 for name_value from filepath.
676 * Input: filepath, name_value,
677 * pointer to nb_sample, min, max, sum, sum_square
679 * Output: SUCCESS or ERROR on failure
681 **************************************************************************/
682 int stats_values(char * filepath,char * name_value, unsigned long long * nb_sample, unsigned long long * min, unsigned long long * max, unsigned long long * sum, unsigned long long * sum_square)
685 char line[MAX_LINE_SIZE];
686 int nbReadValues = 0;
688 if( (statfile=fopen(filepath,"r")) == NULL) {
689 report("stats_value() failed to open %s",filepath);
692 /*find the good line for name_value*/
694 if( fgets(line,MAX_LINE_SIZE,statfile) == NULL ) {
695 report("stats_values() failed to find %s values in %s stat_file",name_value,statfile);
698 } while ( strstr(line,name_value) == NULL );
700 if((nbReadValues=sscanf(line,"%*s %llu %*s %*s %llu %llu %llu %llu",nb_sample,min,max,sum,sum_square)) == 5) {
702 } else if( nbReadValues == 1 && *nb_sample == 0) {
703 *min = *max = *sum = *sum_square = 0;
706 report("stats_values() failed to read stats_values for %s value in %s stat_file",name_value,statfile);
718 /**************************************************************************
719 * Function: mds_stats_values
721 * Description: Setup nb_sample, min, max, sum and sum_square stats values
722 for mds stats name_value .
725 * pointer to nb_sample, min, max, sum, sum_square
727 * Output: SUCCESS or ERROR on failure
729 **************************************************************************/
730 extern int mds_stats_values(char * name_value, unsigned long long * nb_sample, unsigned long long * min, unsigned long long * max, unsigned long long * sum, unsigned long long * sum_square)
732 unsigned long long tmp_nb_sample=0,tmp_min=0,tmp_max=0,tmp_sum=0,tmp_sum_square=0;
733 /*we parse the three MDS stat files and sum values*/
734 if( stats_values(FILEPATH_MDS_SERVER_STATS,name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
737 *nb_sample=tmp_nb_sample;
741 *sum_square=tmp_sum_square;
744 if( stats_values(FILEPATH_MDS_SERVER_READPAGE_STATS,name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
747 *nb_sample += tmp_nb_sample;
751 *sum_square += tmp_sum_square;
754 if( stats_values(FILEPATH_MDS_SERVER_SETATTR_STATS,name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
757 *nb_sample += tmp_nb_sample;
761 *sum_square += tmp_sum_square;
767 void convert_ull(counter64 *c64, unsigned long long ull, size_t *var_len)
769 *var_len = sizeof(*c64);
770 c64->low = (unsigned long long) (0x0ffffffff & ull);
772 c64->high = (unsigned long long) (0x0ffffffff & ull);