1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * snmp/lustre-snmp-util.c
38 * Author: PJ Kirner <pjkirner@clusterfs.com>
42 * include important headers
45 #include <net-snmp/net-snmp-config.h>
46 #include <net-snmp/net-snmp-includes.h>
47 #include <net-snmp/agent/net-snmp-agent-includes.h>
53 #include <sys/types.h>
54 #if defined (__linux__)
63 #include "lustre-snmp-util.h"
65 /*********************************************************************
66 * Function: get_file_list
68 * Description: For the given valid directory path, returns the list
69 * all directories or files in that path.
71 * Input: 'dirname' the directory path.
72 * 'file_type' if this takes the value DIR_TYPE then
73 * returns the list of directories in that path.
74 * If its of type FILE_TYPE then returns the list of files
76 * 'count' pointer to number of elements returned in the
79 * Output: List of directories/files in that path.
81 *********************************************************************/
83 char *get_file_list(const char *dirname, int file_type, uint32_t *count)
87 struct dirent *pdirent = NULL;
92 char filename[MAX_PATH_SIZE];
95 if ((dirname == NULL) || ((pdir = opendir(dirname)) == NULL )) {
96 if (dirname == NULL) {
97 report("%s %s:line %d %s", __FILE__, __FUNCTION__, __LINE__,
98 "NULL directory is passed as parameter to funtion");
100 report("%s %s:line %d Error in opening the dir %s", __FILE__,
101 __FUNCTION__, __LINE__, dirname);
109 if ((pdirent = readdir(pdir)) == NULL)
112 /* Skip over '.' and '..' directores */
113 if ((pdirent->d_name[0] == '.') ||
114 !strcmp(pdirent->d_name, FILENAME_NUM_REF))
117 sprintf(filename, "%s/%s", dirname, pdirent->d_name);
118 cond1 = (file_type == FILE_TYPE) && is_directory(filename);
119 cond2 = (file_type == DIR_TYPE) && (!is_directory(filename));
124 /* Calculate the number of bytes for this new entry.*/
125 byte_count += strlen(pdirent->d_name) + 1;
131 if (file_count != 0) {
133 /* need one extra one for the finall NULL terminator*/
134 if ((ret_str = (char *) malloc(byte_count + 1)) == NULL) {
135 report("get_file_list() failed to malloc(%d)",byte_count+1);
142 while (file_count != 0) {
143 if ((pdirent = readdir(pdir)) == NULL)
146 if ((pdirent->d_name[0] == '.') ||
147 !strcmp(pdirent->d_name, FILENAME_NUM_REF))
150 sprintf(filename, "%s/%s", dirname, pdirent->d_name);
151 cond1 = (file_type == FILE_TYPE) && is_directory(filename);
152 cond2 = (file_type == DIR_TYPE) && (!is_directory(filename));
157 strcpy(ret_str + curr_offset, pdirent->d_name);
158 curr_offset = curr_offset + strlen(pdirent->d_name) + 1;
161 /* Put in the finall null terminator*/
162 ret_str[byte_count] = '\0';
169 /*********************************************************************
170 * Function: is_directory
172 * Description: Checks if given filename is a directory or not.
173 * all directories or files in that path.
175 * Input: 'filename' the directory path to be checked.
177 * Output: Returns 1 if its a directory else 0.
179 *********************************************************************/
181 int is_directory(const char *filename)
187 result = stat(filename, &statf);
188 return ((result == SUCCESS) && (statf.st_mode & S_IFDIR));
191 /*********************************************************************
192 * Function: read_string
194 * Description: For the given valid file path, reads the data in
197 * Input: 'filepath' the file whose data is to be accessed.
198 * 'lustre_var' the data from the file is read into
199 * this variable, returned to the requestor.
200 * 'var_max_size' the max size of the string
201 * 'report_error' boolean if error should be reported on
204 * Output: Returns SUCCESS if read successfully from file else
206 *********************************************************************/
208 int read_string(const char *filepath, char *lustre_var, size_t var_max_size)
212 int ret_val = SUCCESS;
213 int report_error = 1;
215 if ((filepath == NULL) || (lustre_var == NULL)) {
216 report("%s %s:line %d %s", __FILE__, __FUNCTION__, __LINE__,
217 "Input parameter is NULL");
220 fptr = fopen(filepath, "r");
224 report("%s %s:line %d Unable to open the file %s", __FILE__,
225 __FUNCTION__, __LINE__, filepath);
228 if (fgets(lustre_var, var_max_size, fptr) == NULL) {
229 report("%s %s:line %d read failed for file %s", __FILE__,
230 __FUNCTION__, __LINE__, filepath);
233 len = strlen(lustre_var);
235 Last char is EOF, before string ends,
236 so '\0' is moved to last but one.
238 lustre_var[len-1] = lustre_var[len];
246 /**************************************************************************
247 * Function: lustrefs_ctrl
249 * Description: Execute /etc/init.d/lustre script for starting,
250 * stopping and restarting Lustre services in child process.
252 * Input: Start/Stop/Restart Command Number.
253 * Output: Returns void
255 **************************************************************************/
257 void lustrefs_ctrl(int command)
261 cmd[0] = LUSTRE_SERVICE;
280 report("failed to execvp(\'%s %s\')",cmd[0],cmd[1]);
285 /*****************************************************************************
286 * Function: get_sysstatus
288 * Description: Read /var/lustre/sysStatus file, and based on file contents
289 * return the status of Lustre services.
292 * Output: Return ONLINE/OFFLINE/ONLINE PENDING/OFFLINE PENDING status
295 ****************************************************************************/
297 int get_sysstatus(void)
299 int ret_val = ERROR ;
300 char sys_status[50] = {0};
302 if(SUCCESS == read_string(FILENAME_SYS_STATUS,sys_status,sizeof(sys_status)))
304 if (memcmp(sys_status, STR_ONLINE_PENDING,strlen(STR_ONLINE_PENDING)) == 0)
305 ret_val = ONLINE_PENDING;
306 else if (memcmp(sys_status, STR_ONLINE, strlen(STR_ONLINE)) == 0)
308 else if (memcmp(sys_status, STR_OFFLINE_PENDING,strlen(STR_OFFLINE_PENDING)) == 0)
309 ret_val = OFFLINE_PENDING;
310 else if (memcmp(sys_status, STR_OFFLINE, strlen(STR_OFFLINE)) == 0)
313 report("%s %s:line %d Bad Contents in file %s \'%s\'", __FILE__,
314 __FUNCTION__, __LINE__, FILENAME_SYS_STATUS,sys_status);
320 /*****************************************************************************
321 * Function: read_ulong
323 * Description: Read long values from lproc and copy to the location
324 * pointed by input parameter.
326 * Input: file path, and pointer for data to be copied
328 * Output: Return ERROR or SUCCESS.
330 ****************************************************************************/
332 int read_ulong(const char *file_path, unsigned long *valuep)
334 char file_data[MAX_LINE_SIZE];
337 if ((ret_val = read_string(file_path, file_data,sizeof(file_data))) == SUCCESS){
338 *valuep = strtoul(file_data,NULL,10);
343 /*****************************************************************************
344 * Function: read_counter64
346 * Description: Read counter64 values from lproc and copy to the location
347 * pointed by input parameter.
349 * Input: file path, and pointer for data to be copied
351 * Output: Return ERROR or SUCCESS.
353 ****************************************************************************/
355 int read_counter64(const char *file_path, counter64 *c64,int factor)
357 char file_data[MAX_LINE_SIZE];
359 unsigned long long tmp = 0;
361 if ((ret_val = read_string(file_path, file_data,sizeof(file_data))) == SUCCESS) {
362 tmp = atoll(file_data) * factor;
363 c64->low = (unsigned long) (0x0FFFFFFFF & tmp);
364 tmp >>= 32; /* Shift right by 4 bytes */
365 c64->high = (unsigned long) (0x0FFFFFFFF & tmp);
370 /*****************************************************************************
371 * Function: get_nth_entry_from_list
373 * Description: Find the n'th entry from a null terminated list of string
375 * Input: dir_list - the list
376 * num - the number of elements in the list
377 * index - the index we are looking for
379 * Output: Return NULL on failure, or the string name on success.
381 ****************************************************************************/
383 const char *get_nth_entry_from_list(const char* dir_list,int num,int index)
390 * if we've reached the end of the list for some reason
391 * because num was wrong then stop processing
393 if( *(dir_list+cur_ptr) == 0)
396 /* If we've found the right one */
398 return dir_list+cur_ptr;
400 /* Move to the next one*/
401 cur_ptr += strlen(dir_list + cur_ptr)+1;
406 /*****************************************************************************
409 * Description: This function used to report error msg to stderr and log into
410 * log file(default file:/var/log/snmpd.log) when agent is started with
411 * debug option -Dlsnmpd
412 * Input: format string and variable arguments.
414 ****************************************************************************/
416 void report(const char *fmt, ...)
421 va_start(arg_list, fmt);
422 vsprintf(buf, fmt, arg_list);
425 DEBUGMSGTL(("lsnmpd", "%s\n", buf));
426 fprintf(stderr, "%s\n", buf);
432 /**************************************************************************
433 * Function: oid_table_ulong_handler
435 * Description: Fetch a unsigned long from the given location.
436 * Setup var_len, and return a pointer to the data.
438 * Input: file_path, and var_len pointer
440 * Output: NULL on failure, or pointer to data
442 **************************************************************************/
445 oid_table_ulong_handler(
446 const char* file_path,
449 static unsigned long ulong_ret;
450 if (SUCCESS != read_ulong(file_path,&ulong_ret))
452 *var_len = sizeof(ulong_ret);
453 return (unsigned char *) &ulong_ret;
456 /**************************************************************************
457 * Function: oid_table_c64_handler
459 * Description: Fetch a counter64 from the given location.
460 * Setup var_len, and return a pointer to the data.
462 * Input: file_path, and var_len pointer
464 * Output: NULL on failure, or pointer to data
466 **************************************************************************/
468 unsigned char* oid_table_c64_handler(const char* file_path,size_t *var_len)
470 static counter64 c64;
471 if (SUCCESS != read_counter64(file_path,&c64,1))
473 *var_len = sizeof(c64);
474 return (unsigned char *) &c64;
477 /**************************************************************************
478 * Function: oid_table_c64_kb_handler
480 * Description: Fetch a counter64 from the given location.
481 * Setup var_len, and return a pointer to the data.
482 * Different than oid_table_c64_handler in that
483 * the original value is multiplied by 1024 before converting
484 * to a counter64. (e.g. turn KB into a Byte scaled value)
486 * Input: file_path, and var_len pointer
488 * Output: NULL on failure, or pointer to data
490 **************************************************************************/
492 unsigned char* oid_table_c64_kb_handler(const char* file_path,size_t *var_len)
494 static counter64 c64;
495 /* scale by factor of 1024*/
496 if (SUCCESS != read_counter64(file_path,&c64,1024))
498 *var_len = sizeof(c64);
499 return (unsigned char *) &c64;
502 /**************************************************************************
503 * Function: oid_table_obj_name_handler
505 * Description: Just copy the file_path and return as the output value.
507 * Input: file_path, and var_len pointer
509 * Output: NULL on failure, or pointer to data
511 **************************************************************************/
514 oid_table_obj_name_handler(
515 const char* file_path,
518 static unsigned char string[SPRINT_MAX_LEN];
519 *var_len = strlen(file_path);
520 *var_len = MIN_LEN(*var_len, sizeof(string));
521 memcpy(string, file_path, *var_len);
522 return (unsigned char *) string;
525 /**************************************************************************
526 * Function: oid_table_string_handler
528 * Description: Fetch a string from the given location.
529 * Setup var_len, and return a pointer to the data.
531 * Input: file_path, and var_len pointer
533 * Output: NULL on failure, or pointer to data
535 **************************************************************************/
538 oid_table_string_handler(
539 const char* file_path,
542 static unsigned char string[SPRINT_MAX_LEN];
543 if( SUCCESS != read_string(file_path, (char *)string,sizeof(string)))
545 *var_len = strlen((char *)string);
546 return (unsigned char *) string;
550 /**************************************************************************
551 * Function: oid_table_is_directory_handler
553 * Description: Determine if the file_path is a directory.
554 * Setup a boolean return value.
555 * Setup var_len, and return a pointer to the data.
557 * Input: file_path, and var_len pointer
559 * Output: NULL on failure, or pointer to data
561 **************************************************************************/
564 oid_table_is_directory_handler(
565 const char* file_path,
568 static long long_ret;
569 long_ret = is_directory(file_path);
570 *var_len = sizeof(long_ret);
571 return (unsigned char *) &long_ret;
574 /**************************************************************************
575 * Function: var_genericTable
577 * Description: Handle Table driven OID processing
579 **************************************************************************/
582 var_genericTable(struct variable *vp,
587 WriteMethod **write_method,
589 struct oid_table *ptable)
594 unsigned char *ret_val = NULL;
596 const char* obj_name;
600 * Get the list of file. If there are no elements
603 if( 0 == (dir_list = get_file_list(path, DIR_TYPE, &num)))
609 if (header_simple_table(vp,name,length,exact,var_len,write_method, num)
611 goto cleanup_and_exit;
614 * The number of the device we're looking at
616 deviceindex = name[*length - 1] - 1;
619 * If we couldn't find this element
620 * something must have recently changed return
623 if(deviceindex >= num){
624 report("deviceindex=%d exceeds number of elements=%d",deviceindex,num);
625 goto cleanup_and_exit;
629 * Fetch the object name from the list
631 obj_name = get_nth_entry_from_list(dir_list,num,deviceindex);
632 if(obj_name == NULL){
634 * Note this should never really happen because we check deviceindex >=num
635 * above. And dir_list should be consitent with num
636 * but just in case...
638 report("object name not found in list",deviceindex,num);
639 goto cleanup_and_exit;
643 * Find the matching magic - or the end of the list
645 while(ptable[i].magic != vp->magic && ptable[i].magic != 0)
649 * If we didn't find a matching entry return
651 if(ptable[i].magic==0)
652 goto cleanup_and_exit;
655 * If the name is NULL is a special case and
656 * just just pass the obj_name as the file_path
657 * otherwise we create a file path from the given components
659 if(ptable[i].name != 0){
660 char file_path[MAX_PATH_SIZE];
661 sprintf(file_path, "%s%s/%s",path,obj_name,ptable[i].name);
662 ret_val = ptable[i].fhandler(file_path,var_len);
665 ret_val = ptable[i].fhandler(obj_name,var_len);
672 /**************************************************************************
673 * Function: stats_values
675 * Description: Setup nb_sample, min, max, sum and sum_square stats values
676 for name_value from filepath.
678 * Input: filepath, name_value,
679 * pointer to nb_sample, min, max, sum, sum_square
681 * Output: SUCCESS or ERROR on failure
683 **************************************************************************/
684 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)
687 char line[MAX_LINE_SIZE];
688 int nbReadValues = 0;
690 if( (statfile=fopen(filepath,"r")) == NULL) {
691 report("stats_value() failed to open %s",filepath);
694 /*find the good line for name_value*/
696 if( fgets(line,MAX_LINE_SIZE,statfile) == NULL ) {
697 report("stats_values() failed to find %s values in %s stat_file",name_value,statfile);
700 } while ( strstr(line,name_value) == NULL );
702 if((nbReadValues=sscanf(line,"%*s %llu %*s %*s %llu %llu %llu %llu",nb_sample,min,max,sum,sum_square)) == 5) {
704 } else if( nbReadValues == 1 && *nb_sample == 0) {
705 *min = *max = *sum = *sum_square = 0;
708 report("stats_values() failed to read stats_values for %s value in %s stat_file",name_value,statfile);
720 /**************************************************************************
721 * Function: mds_stats_values
723 * Description: Setup nb_sample, min, max, sum and sum_square stats values
724 for mds stats name_value .
727 * pointer to nb_sample, min, max, sum, sum_square
729 * Output: SUCCESS or ERROR on failure
731 **************************************************************************/
732 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)
734 unsigned long long tmp_nb_sample=0,tmp_min=0,tmp_max=0,tmp_sum=0,tmp_sum_square=0;
735 /*we parse the three MDS stat files and sum values*/
736 if( stats_values(FILEPATH_MDS_SERVER_STATS,name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
739 *nb_sample=tmp_nb_sample;
743 *sum_square=tmp_sum_square;
746 if( stats_values(FILEPATH_MDS_SERVER_READPAGE_STATS,name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
749 *nb_sample += tmp_nb_sample;
753 *sum_square += tmp_sum_square;
756 if( stats_values(FILEPATH_MDS_SERVER_SETATTR_STATS,name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
759 *nb_sample += tmp_nb_sample;
763 *sum_square += tmp_sum_square;