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/
28 * Lustre is a trademark of Sun Microsystems, Inc.
30 * snmp/lustre-snmp-util.c
32 * Author: PJ Kirner <pjkirner@clusterfs.com>
36 * include important headers
39 #include <net-snmp/net-snmp-config.h>
40 #include <net-snmp/net-snmp-includes.h>
41 #include <net-snmp/agent/net-snmp-agent-includes.h>
47 #include <sys/types.h>
55 #include "lustre-snmp-util.h"
57 /*********************************************************************
58 * Function: get_file_list
60 * Description: For the given valid directory path, returns the list
61 * all directories or files in that path.
63 * Input: 'dirname' the directory path.
64 * 'file_type' if this takes the value DIR_TYPE then
65 * returns the list of directories in that path.
66 * If its of type FILE_TYPE then returns the list of files
68 * 'count' pointer to number of elements returned in the
71 * Output: List of directories/files in that path.
73 *********************************************************************/
75 char *get_file_list(const char *dirname, int file_type, uint32_t *count)
79 struct dirent *pdirent = NULL;
84 char filename[MAX_PATH_SIZE];
87 if ((dirname == NULL) || ((pdir = opendir(dirname)) == NULL )) {
88 if (dirname == NULL) {
89 report("%s %s:line %d %s", __FILE__, __FUNCTION__, __LINE__,
90 "NULL directory is passed as parameter to funtion");
92 report("%s %s:line %d Error in opening the dir %s", __FILE__,
93 __FUNCTION__, __LINE__, dirname);
101 if ((pdirent = readdir(pdir)) == NULL)
104 /* Skip over '.' and '..' directores */
105 if ((pdirent->d_name[0] == '.') ||
106 !strcmp(pdirent->d_name, FILENAME_NUM_REF))
109 sprintf(filename, "%s/%s", dirname, pdirent->d_name);
110 cond1 = (file_type == FILE_TYPE) && is_directory(filename);
111 cond2 = (file_type == DIR_TYPE) && (!is_directory(filename));
116 /* Calculate the number of bytes for this new entry.*/
117 byte_count += strlen(pdirent->d_name) + 1;
123 if (file_count != 0) {
125 /* need one extra one for the finall NULL terminator*/
126 if ((ret_str = (char *) malloc(byte_count + 1)) == NULL) {
127 report("get_file_list() failed to malloc(%d)",byte_count+1);
134 while (file_count != 0) {
135 if ((pdirent = readdir(pdir)) == NULL)
138 if ((pdirent->d_name[0] == '.') ||
139 !strcmp(pdirent->d_name, FILENAME_NUM_REF))
142 sprintf(filename, "%s/%s", dirname, pdirent->d_name);
143 cond1 = (file_type == FILE_TYPE) && is_directory(filename);
144 cond2 = (file_type == DIR_TYPE) && (!is_directory(filename));
149 strcpy(ret_str + curr_offset, pdirent->d_name);
150 curr_offset = curr_offset + strlen(pdirent->d_name) + 1;
153 /* Put in the finall null terminator*/
154 ret_str[byte_count] = '\0';
161 /*********************************************************************
162 * Function: is_directory
164 * Description: Checks if given filename is a directory or not.
165 * all directories or files in that path.
167 * Input: 'filename' the directory path to be checked.
169 * Output: Returns 1 if its a directory else 0.
171 *********************************************************************/
173 int is_directory(const char *filename)
179 result = stat(filename, &statf);
180 return ((result == SUCCESS) && (statf.st_mode & S_IFDIR));
183 /*********************************************************************
184 * Function: read_string
186 * Description: For the given valid file path, reads the data in
189 * Input: 'filepath' the file whose data is to be accessed.
190 * 'lustre_var' the data from the file is read into
191 * this variable, returned to the requestor.
192 * 'var_max_size' the max size of the string
193 * 'report_error' boolean if error should be reported on
196 * Output: Returns SUCCESS if read successfully from file else
198 *********************************************************************/
200 int read_string(const char *filepath, char *lustre_var, size_t var_max_size)
204 int ret_val = SUCCESS;
205 int report_error = 1;
207 if ((filepath == NULL) || (lustre_var == NULL)) {
208 report("%s %s:line %d %s", __FILE__, __FUNCTION__, __LINE__,
209 "Input parameter is NULL");
212 fptr = fopen(filepath, "r");
216 report("%s %s:line %d Unable to open the file %s", __FILE__,
217 __FUNCTION__, __LINE__, filepath);
220 if (fgets(lustre_var, var_max_size, fptr) == NULL) {
221 report("%s %s:line %d read failed for file %s", __FILE__,
222 __FUNCTION__, __LINE__, filepath);
225 len = strlen(lustre_var);
227 Last char is EOF, before string ends,
228 so '\0' is moved to last but one.
230 lustre_var[len-1] = lustre_var[len];
238 /**************************************************************************
239 * Function: lustrefs_ctrl
241 * Description: Execute /etc/init.d/lustre script for starting,
242 * stopping and restarting Lustre services in child process.
244 * Input: Start/Stop/Restart Command Number.
245 * Output: Returns void
247 **************************************************************************/
249 void lustrefs_ctrl(int command)
253 cmd[0] = LUSTRE_SERVICE;
272 report("failed to execvp(\'%s %s\')",cmd[0],cmd[1]);
277 /*****************************************************************************
278 * Function: get_sysstatus
280 * Description: Read /var/lustre/sysStatus file, and based on file contents
281 * return the status of Lustre services.
284 * Output: Return ONLINE/OFFLINE/ONLINE PENDING/OFFLINE PENDING status
287 ****************************************************************************/
289 int get_sysstatus(void)
291 int ret_val = ERROR ;
292 char sys_status[50] = {0};
294 if(SUCCESS == read_string(FILENAME_SYS_STATUS,sys_status,sizeof(sys_status)))
296 if (memcmp(sys_status, STR_ONLINE_PENDING,strlen(STR_ONLINE_PENDING)) == 0)
297 ret_val = ONLINE_PENDING;
298 else if (memcmp(sys_status, STR_ONLINE, strlen(STR_ONLINE)) == 0)
300 else if (memcmp(sys_status, STR_OFFLINE_PENDING,strlen(STR_OFFLINE_PENDING)) == 0)
301 ret_val = OFFLINE_PENDING;
302 else if (memcmp(sys_status, STR_OFFLINE, strlen(STR_OFFLINE)) == 0)
305 report("%s %s:line %d Bad Contents in file %s \'%s\'", __FILE__,
306 __FUNCTION__, __LINE__, FILENAME_SYS_STATUS,sys_status);
312 /*****************************************************************************
313 * Function: read_ulong
315 * Description: Read long values from lproc and copy to the location
316 * pointed by input parameter.
318 * Input: file path, and pointer for data to be copied
320 * Output: Return ERROR or SUCCESS.
322 ****************************************************************************/
324 int read_ulong(const char *file_path, unsigned long *valuep)
326 char file_data[MAX_LINE_SIZE];
329 if ((ret_val = read_string(file_path, file_data,sizeof(file_data))) == SUCCESS){
330 *valuep = strtoul(file_data,NULL,10);
335 /*****************************************************************************
336 * Function: read_counter64
338 * Description: Read counter64 values from lproc and copy to the location
339 * pointed by input parameter.
341 * Input: file path, and pointer for data to be copied
343 * Output: Return ERROR or SUCCESS.
345 ****************************************************************************/
347 int read_counter64(const char *file_path, counter64 *c64,int factor)
349 char file_data[MAX_LINE_SIZE];
351 unsigned long long tmp = 0;
353 if ((ret_val = read_string(file_path, file_data,sizeof(file_data))) == SUCCESS) {
354 tmp = atoll(file_data) * factor;
355 c64->low = (unsigned long) (0x0FFFFFFFF & tmp);
356 tmp >>= 32; /* Shift right by 4 bytes */
357 c64->high = (unsigned long) (0x0FFFFFFFF & tmp);
362 /*****************************************************************************
363 * Function: get_nth_entry_from_list
365 * Description: Find the n'th entry from a null terminated list of string
367 * Input: dir_list - the list
368 * num - the number of elements in the list
369 * index - the index we are looking for
371 * Output: Return NULL on failure, or the string name on success.
373 ****************************************************************************/
375 const char *get_nth_entry_from_list(const char* dir_list,int num,int index)
382 * if we've reached the end of the list for some reason
383 * because num was wrong then stop processing
385 if( *(dir_list+cur_ptr) == 0)
388 /* If we've found the right one */
390 return dir_list+cur_ptr;
392 /* Move to the next one*/
393 cur_ptr += strlen(dir_list + cur_ptr)+1;
398 /*****************************************************************************
401 * Description: This function used to report error msg to stderr and log into
402 * log file(default file:/var/log/snmpd.log) when agent is started with
403 * debug option -Dlsnmpd
404 * Input: format string and variable arguments.
406 ****************************************************************************/
408 void report(const char *fmt, ...)
413 va_start(arg_list, fmt);
414 vsprintf(buf, fmt, arg_list);
417 DEBUGMSGTL(("lsnmpd", "%s\n", buf));
418 fprintf(stderr, "%s\n", buf);
424 /**************************************************************************
425 * Function: oid_table_ulong_handler
427 * Description: Fetch a unsigned long from the given location.
428 * Setup var_len, and return a pointer to the data.
430 * Input: file_path, and var_len pointer
432 * Output: NULL on failure, or pointer to data
434 **************************************************************************/
437 oid_table_ulong_handler(
438 const char* file_path,
441 static unsigned long ulong_ret;
442 if (SUCCESS != read_ulong(file_path,&ulong_ret))
444 *var_len = sizeof(ulong_ret);
445 return (unsigned char *) &ulong_ret;
448 /**************************************************************************
449 * Function: oid_table_c64_handler
451 * Description: Fetch a counter64 from the given location.
452 * Setup var_len, and return a pointer to the data.
454 * Input: file_path, and var_len pointer
456 * Output: NULL on failure, or pointer to data
458 **************************************************************************/
460 unsigned char* oid_table_c64_handler(const char* file_path,size_t *var_len)
462 static counter64 c64;
463 if (SUCCESS != read_counter64(file_path,&c64,1))
465 *var_len = sizeof(c64);
466 return (unsigned char *) &c64;
469 /**************************************************************************
470 * Function: oid_table_c64_kb_handler
472 * Description: Fetch a counter64 from the given location.
473 * Setup var_len, and return a pointer to the data.
474 * Different than oid_table_c64_handler in that
475 * the original value is multiplied by 1024 before converting
476 * to a counter64. (e.g. turn KB into a Byte scaled value)
478 * Input: file_path, and var_len pointer
480 * Output: NULL on failure, or pointer to data
482 **************************************************************************/
484 unsigned char* oid_table_c64_kb_handler(const char* file_path,size_t *var_len)
486 static counter64 c64;
487 /* scale by factor of 1024*/
488 if (SUCCESS != read_counter64(file_path,&c64,1024))
490 *var_len = sizeof(c64);
491 return (unsigned char *) &c64;
494 /**************************************************************************
495 * Function: oid_table_obj_name_handler
497 * Description: Just copy the file_path and return as the output value.
499 * Input: file_path, and var_len pointer
501 * Output: NULL on failure, or pointer to data
503 **************************************************************************/
506 oid_table_obj_name_handler(
507 const char* file_path,
510 static unsigned char string[SPRINT_MAX_LEN];
511 *var_len = strlen(file_path);
512 *var_len = MIN_LEN(*var_len, sizeof(string));
513 memcpy(string, file_path, *var_len);
514 return (unsigned char *) string;
517 /**************************************************************************
518 * Function: oid_table_string_handler
520 * Description: Fetch a string from the given location.
521 * Setup var_len, and return a pointer to the data.
523 * Input: file_path, and var_len pointer
525 * Output: NULL on failure, or pointer to data
527 **************************************************************************/
530 oid_table_string_handler(
531 const char* file_path,
534 static unsigned char string[SPRINT_MAX_LEN];
535 if( SUCCESS != read_string(file_path, (char *)string,sizeof(string)))
537 *var_len = strlen((char *)string);
538 return (unsigned char *) string;
542 /**************************************************************************
543 * Function: oid_table_is_directory_handler
545 * Description: Determine if the file_path is a directory.
546 * Setup a boolean return value.
547 * Setup var_len, and return a pointer to the data.
549 * Input: file_path, and var_len pointer
551 * Output: NULL on failure, or pointer to data
553 **************************************************************************/
556 oid_table_is_directory_handler(
557 const char* file_path,
560 static long long_ret;
561 long_ret = is_directory(file_path);
562 *var_len = sizeof(long_ret);
563 return (unsigned char *) &long_ret;
566 /**************************************************************************
567 * Function: var_genericTable
569 * Description: Handle Table driven OID processing
571 **************************************************************************/
574 var_genericTable(struct variable *vp,
579 WriteMethod **write_method,
581 struct oid_table *ptable)
586 unsigned char *ret_val = NULL;
588 const char* obj_name;
592 * Get the list of file. If there are no elements
595 if( 0 == (dir_list = get_file_list(path, DIR_TYPE, &num)))
601 if (header_simple_table(vp,name,length,exact,var_len,write_method, num)
603 goto cleanup_and_exit;
606 * The number of the device we're looking at
608 deviceindex = name[*length - 1] - 1;
611 * If we couldn't find this element
612 * something must have recently changed return
615 if(deviceindex >= num){
616 report("deviceindex=%d exceeds number of elements=%d",deviceindex,num);
617 goto cleanup_and_exit;
621 * Fetch the object name from the list
623 obj_name = get_nth_entry_from_list(dir_list,num,deviceindex);
624 if(obj_name == NULL){
626 * Note this should never really happen because we check deviceindex >=num
627 * above. And dir_list should be consitent with num
628 * but just in case...
630 report("object name not found in list",deviceindex,num);
631 goto cleanup_and_exit;
635 * Find the matching magic - or the end of the list
637 while(ptable[i].magic != vp->magic && ptable[i].magic != 0)
641 * If we didn't find a matching entry return
643 if(ptable[i].magic==0)
644 goto cleanup_and_exit;
647 * If the name is NULL is a special case and
648 * just just pass the obj_name as the file_path
649 * otherwise we create a file path from the given components
651 if(ptable[i].name != 0){
652 char file_path[MAX_PATH_SIZE];
653 sprintf(file_path, "%s%s/%s",path,obj_name,ptable[i].name);
654 ret_val = ptable[i].fhandler(file_path,var_len);
657 ret_val = ptable[i].fhandler(obj_name,var_len);
664 /**************************************************************************
665 * Function: stats_values
667 * Description: Setup nb_sample, min, max, sum and sum_square stats values
668 for name_value from filepath.
670 * Input: filepath, name_value,
671 * pointer to nb_sample, min, max, sum, sum_square
673 * Output: SUCCESS or ERROR on failure
675 **************************************************************************/
676 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)
679 char line[MAX_LINE_SIZE];
680 int nbReadValues = 0;
682 if( (statfile=fopen(filepath,"r")) == NULL) {
683 report("stats_value() failed to open %s",filepath);
686 /*find the good line for name_value*/
688 if( fgets(line,MAX_LINE_SIZE,statfile) == NULL ) {
689 report("stats_values() failed to find %s values in %s stat_file",name_value,statfile);
692 } while ( strstr(line,name_value) == NULL );
694 if((nbReadValues=sscanf(line,"%*s %llu %*s %*s %llu %llu %llu %llu",nb_sample,min,max,sum,sum_square)) == 5) {
696 } else if( nbReadValues == 1 && *nb_sample == 0) {
697 *min = *max = *sum = *sum_square = 0;
700 report("stats_values() failed to read stats_values for %s value in %s stat_file",name_value,statfile);
712 /**************************************************************************
713 * Function: mds_stats_values
715 * Description: Setup nb_sample, min, max, sum and sum_square stats values
716 for mds stats name_value .
719 * pointer to nb_sample, min, max, sum, sum_square
721 * Output: SUCCESS or ERROR on failure
723 **************************************************************************/
724 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)
726 unsigned long long tmp_nb_sample=0,tmp_min=0,tmp_max=0,tmp_sum=0,tmp_sum_square=0;
729 /*we parse the three MDS stat files and sum values*/
730 if (cfs_get_param_paths(&path, "mdt/MDS/mds/stats") != 0)
732 if( stats_values(path.gl_pathv[0],name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
733 cfs_free_param_data(&path);
736 *nb_sample=tmp_nb_sample;
740 *sum_square=tmp_sum_square;
742 cfs_free_param_data(&path);
744 if (cfs_get_param_paths(&path, "mdt/MDS/mds_readpage/stats") != 0)
746 if( stats_values(path.gl_pathv[0],name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
747 cfs_free_param_data(&path);
750 *nb_sample += tmp_nb_sample;
754 *sum_square += tmp_sum_square;
756 cfs_free_param_data(&path);
758 if (cfs_get_param_paths(&path, "mdt/MDS/mds_setattr/stats") != 0)
760 if( stats_values(path.gl_pathv[0],name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
761 cfs_free_param_data(&path);
764 *nb_sample += tmp_nb_sample;
768 *sum_square += tmp_sum_square;
770 cfs_free_param_data(&path);
775 void convert_ull(counter64 *c64, unsigned long long ull, size_t *var_len)
777 *var_len = sizeof(*c64);
778 c64->low = (unsigned long long) (0x0ffffffff & ull);
780 c64->high = (unsigned long long) (0x0ffffffff & ull);