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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
27 * This file is part of Lustre, http://www.lustre.org/
29 * snmp/lustre-snmp-util.c
31 * Author: PJ Kirner <pjkirner@clusterfs.com>
35 * include important headers
38 #include <net-snmp/net-snmp-config.h>
39 #include <net-snmp/net-snmp-includes.h>
40 #include <net-snmp/agent/net-snmp-agent-includes.h>
46 #include <sys/types.h>
54 #include "lustre-snmp-util.h"
56 /*********************************************************************
57 * Function: get_file_list
59 * Description: For the given valid directory path, returns the list
60 * all directories or files in that path.
62 * Input: 'dirname' the directory path.
63 * 'file_type' if this takes the value DIR_TYPE then
64 * returns the list of directories in that path.
65 * If its of type FILE_TYPE then returns the list of files
67 * 'count' pointer to number of elements returned in the
70 * Output: List of directories/files in that path.
72 *********************************************************************/
74 char *get_file_list(const char *dirname, int file_type, uint32_t *count)
78 struct dirent *pdirent = NULL;
83 char filename[MAX_PATH_SIZE];
86 if ((dirname == NULL) || ((pdir = opendir(dirname)) == NULL )) {
87 if (dirname == NULL) {
88 report("%s %s:line %d %s", __FILE__, __FUNCTION__, __LINE__,
89 "NULL directory is passed as parameter to funtion");
91 report("%s %s:line %d Error in opening the dir %s", __FILE__,
92 __FUNCTION__, __LINE__, dirname);
100 if ((pdirent = readdir(pdir)) == NULL)
103 /* Skip over '.' and '..' directores */
104 if ((pdirent->d_name[0] == '.') ||
105 !strcmp(pdirent->d_name, FILENAME_NUM_REF))
108 sprintf(filename, "%s/%s", dirname, pdirent->d_name);
109 cond1 = (file_type == FILE_TYPE) && is_directory(filename);
110 cond2 = (file_type == DIR_TYPE) && (!is_directory(filename));
115 /* Calculate the number of bytes for this new entry.*/
116 byte_count += strlen(pdirent->d_name) + 1;
122 if (file_count != 0) {
124 /* need one extra one for the finall NULL terminator*/
125 if ((ret_str = (char *) malloc(byte_count + 1)) == NULL) {
126 report("get_file_list() failed to malloc(%d)",byte_count+1);
133 while (file_count != 0) {
134 if ((pdirent = readdir(pdir)) == NULL)
137 if ((pdirent->d_name[0] == '.') ||
138 !strcmp(pdirent->d_name, FILENAME_NUM_REF))
141 sprintf(filename, "%s/%s", dirname, pdirent->d_name);
142 cond1 = (file_type == FILE_TYPE) && is_directory(filename);
143 cond2 = (file_type == DIR_TYPE) && (!is_directory(filename));
148 strcpy(ret_str + curr_offset, pdirent->d_name);
149 curr_offset = curr_offset + strlen(pdirent->d_name) + 1;
152 /* Put in the finall null terminator*/
153 ret_str[byte_count] = '\0';
160 /*********************************************************************
161 * Function: is_directory
163 * Description: Checks if given filename is a directory or not.
164 * all directories or files in that path.
166 * Input: 'filename' the directory path to be checked.
168 * Output: Returns 1 if its a directory else 0.
170 *********************************************************************/
172 int is_directory(const char *filename)
178 result = stat(filename, &statf);
179 return ((result == SUCCESS) && (statf.st_mode & S_IFDIR));
182 /*********************************************************************
183 * Function: read_string
185 * Description: For the given valid file path, reads the data in
188 * Input: 'filepath' the file whose data is to be accessed.
189 * 'lustre_var' the data from the file is read into
190 * this variable, returned to the requestor.
191 * 'var_max_size' the max size of the string
192 * 'report_error' boolean if error should be reported on
195 * Output: Returns SUCCESS if read successfully from file else
197 *********************************************************************/
199 int read_string(const char *filepath, char *lustre_var, size_t var_max_size)
203 int ret_val = SUCCESS;
204 int report_error = 1;
206 if ((filepath == NULL) || (lustre_var == NULL)) {
207 report("%s %s:line %d %s", __FILE__, __FUNCTION__, __LINE__,
208 "Input parameter is NULL");
211 fptr = fopen(filepath, "r");
215 report("%s %s:line %d Unable to open the file %s", __FILE__,
216 __FUNCTION__, __LINE__, filepath);
219 if (fgets(lustre_var, var_max_size, fptr) == NULL) {
220 report("%s %s:line %d read failed for file %s", __FILE__,
221 __FUNCTION__, __LINE__, filepath);
224 len = strlen(lustre_var);
226 Last char is EOF, before string ends,
227 so '\0' is moved to last but one.
229 lustre_var[len-1] = lustre_var[len];
237 /**************************************************************************
238 * Function: lustrefs_ctrl
240 * Description: Execute /etc/init.d/lustre script for starting,
241 * stopping and restarting Lustre services in child process.
243 * Input: Start/Stop/Restart Command Number.
244 * Output: Returns void
246 **************************************************************************/
248 void lustrefs_ctrl(int command)
252 cmd[0] = LUSTRE_SERVICE;
271 report("failed to execvp(\'%s %s\')",cmd[0],cmd[1]);
275 /*****************************************************************************
276 * Function: get_sysstatus
278 * Description: Read /var/lustre/sysStatus file, and based on file contents
279 * return the status of Lustre services.
282 * Output: Return ONLINE/OFFLINE/ONLINE PENDING/OFFLINE PENDING status
285 ****************************************************************************/
287 int get_sysstatus(void)
289 int ret_val = ERROR ;
290 char sys_status[50] = {0};
292 if(SUCCESS == read_string(FILENAME_SYS_STATUS,sys_status,sizeof(sys_status)))
294 if (memcmp(sys_status, STR_ONLINE_PENDING,strlen(STR_ONLINE_PENDING)) == 0)
295 ret_val = ONLINE_PENDING;
296 else if (memcmp(sys_status, STR_ONLINE, strlen(STR_ONLINE)) == 0)
298 else if (memcmp(sys_status, STR_OFFLINE_PENDING,strlen(STR_OFFLINE_PENDING)) == 0)
299 ret_val = OFFLINE_PENDING;
300 else if (memcmp(sys_status, STR_OFFLINE, strlen(STR_OFFLINE)) == 0)
303 report("%s %s:line %d Bad Contents in file %s \'%s\'", __FILE__,
304 __FUNCTION__, __LINE__, FILENAME_SYS_STATUS,sys_status);
310 /*****************************************************************************
311 * Function: read_ulong
313 * Description: Read long values from lproc and copy to the location
314 * pointed by input parameter.
316 * Input: file path, and pointer for data to be copied
318 * Output: Return ERROR or SUCCESS.
320 ****************************************************************************/
322 int read_ulong(const char *file_path, unsigned long *valuep)
324 char file_data[MAX_LINE_SIZE];
327 if ((ret_val = read_string(file_path, file_data,sizeof(file_data))) == SUCCESS){
328 *valuep = strtoul(file_data,NULL,10);
333 /*****************************************************************************
334 * Function: read_counter64
336 * Description: Read counter64 values from lproc and copy to the location
337 * pointed by input parameter.
339 * Input: file path, and pointer for data to be copied
341 * Output: Return ERROR or SUCCESS.
343 ****************************************************************************/
345 int read_counter64(const char *file_path, counter64 *c64,int factor)
347 char file_data[MAX_LINE_SIZE];
349 unsigned long long tmp = 0;
351 if ((ret_val = read_string(file_path, file_data,sizeof(file_data))) == SUCCESS) {
352 tmp = atoll(file_data) * factor;
353 c64->low = (unsigned long) (0x0FFFFFFFF & tmp);
354 tmp >>= 32; /* Shift right by 4 bytes */
355 c64->high = (unsigned long) (0x0FFFFFFFF & tmp);
360 /*****************************************************************************
361 * Function: get_nth_entry_from_list
363 * Description: Find the n'th entry from a null terminated list of string
365 * Input: dir_list - the list
366 * num - the number of elements in the list
367 * index - the index we are looking for
369 * Output: Return NULL on failure, or the string name on success.
371 ****************************************************************************/
373 const char *get_nth_entry_from_list(const char* dir_list,int num,int index)
380 * if we've reached the end of the list for some reason
381 * because num was wrong then stop processing
383 if( *(dir_list+cur_ptr) == 0)
386 /* If we've found the right one */
388 return dir_list+cur_ptr;
390 /* Move to the next one*/
391 cur_ptr += strlen(dir_list + cur_ptr)+1;
396 /*****************************************************************************
399 * Description: This function used to report error msg to stderr and log into
400 * log file(default file:/var/log/snmpd.log) when agent is started with
401 * debug option -Dlsnmpd
402 * Input: format string and variable arguments.
404 ****************************************************************************/
406 void report(const char *fmt, ...)
411 va_start(arg_list, fmt);
412 vsprintf(buf, fmt, arg_list);
415 DEBUGMSGTL(("lsnmpd", "%s\n", buf));
416 fprintf(stderr, "%s\n", buf);
421 /**************************************************************************
422 * Function: oid_table_ulong_handler
424 * Description: Fetch a unsigned long from the given location.
425 * Setup var_len, and return a pointer to the data.
427 * Input: file_path, and var_len pointer
429 * Output: NULL on failure, or pointer to data
431 **************************************************************************/
434 oid_table_ulong_handler(
435 const char* file_path,
438 static unsigned long ulong_ret;
439 if (SUCCESS != read_ulong(file_path,&ulong_ret))
441 *var_len = sizeof(ulong_ret);
442 return (unsigned char *) &ulong_ret;
445 /**************************************************************************
446 * Function: oid_table_c64_handler
448 * Description: Fetch a counter64 from the given location.
449 * Setup var_len, and return a pointer to the data.
451 * Input: file_path, and var_len pointer
453 * Output: NULL on failure, or pointer to data
455 **************************************************************************/
457 unsigned char* oid_table_c64_handler(const char* file_path,size_t *var_len)
459 static counter64 c64;
460 if (SUCCESS != read_counter64(file_path,&c64,1))
462 *var_len = sizeof(c64);
463 return (unsigned char *) &c64;
466 /**************************************************************************
467 * Function: oid_table_c64_kb_handler
469 * Description: Fetch a counter64 from the given location.
470 * Setup var_len, and return a pointer to the data.
471 * Different than oid_table_c64_handler in that
472 * the original value is multiplied by 1024 before converting
473 * to a counter64. (e.g. turn KB into a Byte scaled value)
475 * Input: file_path, and var_len pointer
477 * Output: NULL on failure, or pointer to data
479 **************************************************************************/
481 unsigned char* oid_table_c64_kb_handler(const char* file_path,size_t *var_len)
483 static counter64 c64;
484 /* scale by factor of 1024*/
485 if (SUCCESS != read_counter64(file_path,&c64,1024))
487 *var_len = sizeof(c64);
488 return (unsigned char *) &c64;
491 /**************************************************************************
492 * Function: oid_table_obj_name_handler
494 * Description: Just copy the file_path and return as the output value.
496 * Input: file_path, and var_len pointer
498 * Output: NULL on failure, or pointer to data
500 **************************************************************************/
503 oid_table_obj_name_handler(
504 const char* file_path,
507 static unsigned char string[SPRINT_MAX_LEN];
508 *var_len = strlen(file_path);
509 *var_len = MIN_LEN(*var_len, sizeof(string));
510 memcpy(string, file_path, *var_len);
511 return (unsigned char *) string;
514 /**************************************************************************
515 * Function: oid_table_string_handler
517 * Description: Fetch a string from the given location.
518 * Setup var_len, and return a pointer to the data.
520 * Input: file_path, and var_len pointer
522 * Output: NULL on failure, or pointer to data
524 **************************************************************************/
527 oid_table_string_handler(
528 const char* file_path,
531 static unsigned char string[SPRINT_MAX_LEN];
532 if( SUCCESS != read_string(file_path, (char *)string,sizeof(string)))
534 *var_len = strlen((char *)string);
535 return (unsigned char *) string;
539 /**************************************************************************
540 * Function: oid_table_is_directory_handler
542 * Description: Determine if the file_path is a directory.
543 * Setup a boolean return value.
544 * Setup var_len, and return a pointer to the data.
546 * Input: file_path, and var_len pointer
548 * Output: NULL on failure, or pointer to data
550 **************************************************************************/
553 oid_table_is_directory_handler(
554 const char* file_path,
557 static long long_ret;
558 long_ret = is_directory(file_path);
559 *var_len = sizeof(long_ret);
560 return (unsigned char *) &long_ret;
563 /**************************************************************************
564 * Function: var_genericTable
566 * Description: Handle Table driven OID processing
568 **************************************************************************/
571 var_genericTable(struct variable *vp,
576 WriteMethod **write_method,
578 struct oid_table *ptable)
583 unsigned char *ret_val = NULL;
585 const char* obj_name;
589 * Get the list of file. If there are no elements
592 if( 0 == (dir_list = get_file_list(path, DIR_TYPE, &num)))
598 if (header_simple_table(vp,name,length,exact,var_len,write_method, num)
600 goto cleanup_and_exit;
603 * The number of the device we're looking at
605 deviceindex = name[*length - 1] - 1;
608 * If we couldn't find this element
609 * something must have recently changed return
612 if(deviceindex >= num){
613 report("deviceindex=%d exceeds number of elements=%d",deviceindex,num);
614 goto cleanup_and_exit;
618 * Fetch the object name from the list
620 obj_name = get_nth_entry_from_list(dir_list,num,deviceindex);
621 if(obj_name == NULL){
623 * Note this should never really happen because we check deviceindex >=num
624 * above. And dir_list should be consitent with num
625 * but just in case...
627 report("object name not found in list",deviceindex,num);
628 goto cleanup_and_exit;
632 * Find the matching magic - or the end of the list
634 while(ptable[i].magic != vp->magic && ptable[i].magic != 0)
638 * If we didn't find a matching entry return
640 if(ptable[i].magic==0)
641 goto cleanup_and_exit;
644 * If the name is NULL is a special case and
645 * just just pass the obj_name as the file_path
646 * otherwise we create a file path from the given components
648 if(ptable[i].name != 0){
649 char file_path[MAX_PATH_SIZE];
650 sprintf(file_path, "%s%s/%s",path,obj_name,ptable[i].name);
651 ret_val = ptable[i].fhandler(file_path,var_len);
654 ret_val = ptable[i].fhandler(obj_name,var_len);
661 /**************************************************************************
662 * Function: stats_values
664 * Description: Setup nb_sample, min, max, sum and sum_square stats values
665 for name_value from filepath.
667 * Input: filepath, name_value,
668 * pointer to nb_sample, min, max, sum, sum_square
670 * Output: SUCCESS or ERROR on failure
672 **************************************************************************/
673 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)
676 char line[MAX_LINE_SIZE];
677 int nbReadValues = 0;
679 if( (statfile=fopen(filepath,"r")) == NULL) {
680 report("stats_value() failed to open %s",filepath);
683 /*find the good line for name_value*/
685 if( fgets(line,MAX_LINE_SIZE,statfile) == NULL ) {
686 report("stats_values() failed to find %s values in %s stat_file",name_value,statfile);
689 } while ( strstr(line,name_value) == NULL );
691 if((nbReadValues=sscanf(line,"%*s %llu %*s %*s %llu %llu %llu %llu",nb_sample,min,max,sum,sum_square)) == 5) {
693 } else if( nbReadValues == 1 && *nb_sample == 0) {
694 *min = *max = *sum = *sum_square = 0;
697 report("stats_values() failed to read stats_values for %s value in %s stat_file",name_value,statfile);
709 /**************************************************************************
710 * Function: mds_stats_values
712 * Description: Setup nb_sample, min, max, sum and sum_square stats values
713 for mds stats name_value .
716 * pointer to nb_sample, min, max, sum, sum_square
718 * Output: SUCCESS or ERROR on failure
720 **************************************************************************/
721 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)
723 unsigned long long tmp_nb_sample=0,tmp_min=0,tmp_max=0,tmp_sum=0,tmp_sum_square=0;
726 /*we parse the three MDS stat files and sum values*/
727 if (cfs_get_param_paths(&path, "mdt/MDS/mds/stats") != 0)
729 if( stats_values(path.gl_pathv[0],name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
730 cfs_free_param_data(&path);
733 *nb_sample=tmp_nb_sample;
737 *sum_square=tmp_sum_square;
739 cfs_free_param_data(&path);
741 if (cfs_get_param_paths(&path, "mdt/MDS/mds_readpage/stats") != 0)
743 if( stats_values(path.gl_pathv[0],name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
744 cfs_free_param_data(&path);
747 *nb_sample += tmp_nb_sample;
751 *sum_square += tmp_sum_square;
753 cfs_free_param_data(&path);
755 if (cfs_get_param_paths(&path, "mdt/MDS/mds_setattr/stats") != 0)
757 if( stats_values(path.gl_pathv[0],name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
758 cfs_free_param_data(&path);
761 *nb_sample += tmp_nb_sample;
765 *sum_square += tmp_sum_square;
767 cfs_free_param_data(&path);
772 void convert_ull(counter64 *c64, unsigned long long ull, size_t *var_len)
774 *var_len = sizeof(*c64);
775 c64->low = (unsigned long long) (0x0ffffffff & ull);
777 c64->high = (unsigned long long) (0x0ffffffff & ull);