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)
301 int ret_val = ERROR ;
302 char sys_status[50] = {0};
304 if(SUCCESS == read_string(FILENAME_SYS_STATUS,sys_status,sizeof(sys_status)))
306 if (memcmp(sys_status, STR_ONLINE_PENDING,strlen(STR_ONLINE_PENDING)) == 0)
307 ret_val = ONLINE_PENDING;
308 else if (memcmp(sys_status, STR_ONLINE, strlen(STR_ONLINE)) == 0)
310 else if (memcmp(sys_status, STR_OFFLINE_PENDING,strlen(STR_OFFLINE_PENDING)) == 0)
311 ret_val = OFFLINE_PENDING;
312 else if (memcmp(sys_status, STR_OFFLINE, strlen(STR_OFFLINE)) == 0)
315 report("%s %s:line %d Bad Contents in file %s \'%s\'", __FILE__,
316 __FUNCTION__, __LINE__, FILENAME_SYS_STATUS,sys_status);
322 /*****************************************************************************
323 * Function: read_ulong
325 * Description: Read long values from lproc and copy to the location
326 * pointed by input parameter.
328 * Input: file path, and pointer for data to be copied
330 * Output: Return ERROR or SUCCESS.
332 ****************************************************************************/
334 int read_ulong(const char *file_path, unsigned long *valuep)
336 char file_data[MAX_LINE_SIZE];
339 if ((ret_val = read_string(file_path, file_data,sizeof(file_data))) == SUCCESS){
340 *valuep = strtoul(file_data,NULL,10);
345 /*****************************************************************************
346 * Function: read_counter64
348 * Description: Read counter64 values from lproc and copy to the location
349 * pointed by input parameter.
351 * Input: file path, and pointer for data to be copied
353 * Output: Return ERROR or SUCCESS.
355 ****************************************************************************/
357 int read_counter64(const char *file_path, counter64 *c64,int factor)
359 char file_data[MAX_LINE_SIZE];
361 unsigned long long tmp = 0;
363 if ((ret_val = read_string(file_path, file_data,sizeof(file_data))) == SUCCESS) {
364 tmp = atoll(file_data) * factor;
365 c64->low = (unsigned long) (0x0FFFFFFFF & tmp);
366 tmp >>= 32; /* Shift right by 4 bytes */
367 c64->high = (unsigned long) (0x0FFFFFFFF & tmp);
372 /*****************************************************************************
373 * Function: get_nth_entry_from_list
375 * Description: Find the n'th entry from a null terminated list of string
377 * Input: dir_list - the list
378 * num - the number of elements in the list
379 * index - the index we are looking for
381 * Output: Return NULL on failure, or the string name on success.
383 ****************************************************************************/
385 const char *get_nth_entry_from_list(const char* dir_list,int num,int index)
392 * if we've reached the end of the list for some reason
393 * because num was wrong then stop processing
395 if( *(dir_list+cur_ptr) == 0)
398 /* If we've found the right one */
400 return dir_list+cur_ptr;
402 /* Move to the next one*/
403 cur_ptr += strlen(dir_list + cur_ptr)+1;
408 /*****************************************************************************
411 * Description: This function used to report error msg to stderr and log into
412 * log file(default file:/var/log/snmpd.log) when agent is started with
413 * debug option -Dlsnmpd
414 * Input: format string and variable arguments.
416 ****************************************************************************/
418 void report(const char *fmt, ...)
423 va_start(arg_list, fmt);
424 vsprintf(buf, fmt, arg_list);
427 DEBUGMSGTL(("lsnmpd", "%s\n", buf));
428 fprintf(stderr, "%s\n", buf);
434 /**************************************************************************
435 * Function: oid_table_ulong_handler
437 * Description: Fetch a unsigned long from the given location.
438 * Setup var_len, and return a pointer to the data.
440 * Input: file_path, and var_len pointer
442 * Output: NULL on failure, or pointer to data
444 **************************************************************************/
447 oid_table_ulong_handler(
448 const char* file_path,
451 static unsigned long ulong_ret;
452 if (SUCCESS != read_ulong(file_path,&ulong_ret))
454 *var_len = sizeof(ulong_ret);
455 return (unsigned char *) &ulong_ret;
458 /**************************************************************************
459 * Function: oid_table_c64_handler
461 * Description: Fetch a counter64 from the given location.
462 * Setup var_len, and return a pointer to the data.
464 * Input: file_path, and var_len pointer
466 * Output: NULL on failure, or pointer to data
468 **************************************************************************/
470 unsigned char* oid_table_c64_handler(const char* file_path,size_t *var_len)
472 static counter64 c64;
473 if (SUCCESS != read_counter64(file_path,&c64,1))
475 *var_len = sizeof(c64);
476 return (unsigned char *) &c64;
479 /**************************************************************************
480 * Function: oid_table_c64_kb_handler
482 * Description: Fetch a counter64 from the given location.
483 * Setup var_len, and return a pointer to the data.
484 * Different than oid_table_c64_handler in that
485 * the original value is multiplied by 1024 before converting
486 * to a counter64. (e.g. turn KB into a Byte scaled value)
488 * Input: file_path, and var_len pointer
490 * Output: NULL on failure, or pointer to data
492 **************************************************************************/
494 unsigned char* oid_table_c64_kb_handler(const char* file_path,size_t *var_len)
496 static counter64 c64;
497 /* scale by factor of 1024*/
498 if (SUCCESS != read_counter64(file_path,&c64,1024))
500 *var_len = sizeof(c64);
501 return (unsigned char *) &c64;
504 /**************************************************************************
505 * Function: oid_table_obj_name_handler
507 * Description: Just copy the file_path and return as the output value.
509 * Input: file_path, and var_len pointer
511 * Output: NULL on failure, or pointer to data
513 **************************************************************************/
516 oid_table_obj_name_handler(
517 const char* file_path,
520 static unsigned char string[SPRINT_MAX_LEN];
521 *var_len = strlen(file_path);
522 *var_len = MIN_LEN(*var_len, sizeof(string));
523 memcpy(string, file_path, *var_len);
524 return (unsigned char *) string;
527 /**************************************************************************
528 * Function: oid_table_string_handler
530 * Description: Fetch a string from the given location.
531 * Setup var_len, and return a pointer to the data.
533 * Input: file_path, and var_len pointer
535 * Output: NULL on failure, or pointer to data
537 **************************************************************************/
540 oid_table_string_handler(
541 const char* file_path,
544 static unsigned char string[SPRINT_MAX_LEN];
545 if( SUCCESS != read_string(file_path, string,sizeof(string)))
547 *var_len = strlen(string);
548 return (unsigned char *) string;
552 /**************************************************************************
553 * Function: oid_table_is_directory_handler
555 * Description: Determine if the file_path is a directory.
556 * Setup a boolean return value.
557 * Setup var_len, and return a pointer to the data.
559 * Input: file_path, and var_len pointer
561 * Output: NULL on failure, or pointer to data
563 **************************************************************************/
566 oid_table_is_directory_handler(
567 const char* file_path,
570 static long long_ret;
571 long_ret = is_directory(file_path);
572 *var_len = sizeof(long_ret);
573 return (unsigned char *) &long_ret;
576 /**************************************************************************
577 * Function: var_genericTable
579 * Description: Handle Table driven OID processing
581 **************************************************************************/
584 var_genericTable(struct variable *vp,
589 WriteMethod **write_method,
591 struct oid_table *ptable)
596 unsigned char *ret_val = NULL;
598 const char* obj_name;
602 * Get the list of file. If there are no elements
605 if( 0 == (dir_list = get_file_list(path, DIR_TYPE, &num)))
611 if (header_simple_table(vp,name,length,exact,var_len,write_method, num)
613 goto cleanup_and_exit;
616 * The number of the device we're looking at
618 deviceindex = name[*length - 1] - 1;
621 * If we couldn't find this element
622 * something must have recently changed return
625 if(deviceindex >= num){
626 report("deviceindex=%d exceeds number of elements=%d",deviceindex,num);
627 goto cleanup_and_exit;
631 * Fetch the object name from the list
633 obj_name = get_nth_entry_from_list(dir_list,num,deviceindex);
634 if(obj_name == NULL){
636 * Note this should never really happen because we check deviceindex >=num
637 * above. And dir_list should be consitent with num
638 * but just in case...
640 report("object name not found in list",deviceindex,num);
641 goto cleanup_and_exit;
645 * Find the matching magic - or the end of the list
647 while(ptable[i].magic != vp->magic && ptable[i].magic != 0)
651 * If we didn't find a matching entry return
653 if(ptable[i].magic==0)
654 goto cleanup_and_exit;
657 * If the name is NULL is a special case and
658 * just just pass the obj_name as the file_path
659 * otherwise we create a file path from the given components
661 if(ptable[i].name != 0){
662 char file_path[MAX_PATH_SIZE];
663 sprintf(file_path, "%s%s/%s",path,obj_name,ptable[i].name);
664 ret_val = ptable[i].fhandler(file_path,var_len);
667 ret_val = ptable[i].fhandler(obj_name,var_len);
674 /**************************************************************************
675 * Function: stats_values
677 * Description: Setup nb_sample, min, max, sum and sum_square stats values
678 for name_value from filepath.
680 * Input: filepath, name_value,
681 * pointer to nb_sample, min, max, sum, sum_square
683 * Output: SUCCESS or ERROR on failure
685 **************************************************************************/
686 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)
689 char line[MAX_LINE_SIZE];
690 int nbReadValues = 0;
692 if( (statfile=fopen(filepath,"r")) == NULL) {
693 report("stats_value() failed to open %s",filepath);
696 /*find the good line for name_value*/
698 if( fgets(line,MAX_LINE_SIZE,statfile) == NULL ) {
699 report("stats_values() failed to find %s values in %s stat_file",name_value,statfile);
702 } while ( strstr(line,name_value) == NULL );
704 if((nbReadValues=sscanf(line,"%*s %llu %*s %*s %llu %llu %llu %llu",nb_sample,min,max,sum,sum_square)) == 5) {
706 } else if( nbReadValues == 1 && *nb_sample == 0) {
707 *min = *max = *sum = *sum_square = 0;
710 report("stats_values() failed to read stats_values for %s value in %s stat_file",name_value,statfile);
722 /**************************************************************************
723 * Function: mds_stats_values
725 * Description: Setup nb_sample, min, max, sum and sum_square stats values
726 for mds stats name_value .
729 * pointer to nb_sample, min, max, sum, sum_square
731 * Output: SUCCESS or ERROR on failure
733 **************************************************************************/
734 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)
736 unsigned long long tmp_nb_sample=0,tmp_min=0,tmp_max=0,tmp_sum=0,tmp_sum_square=0;
737 /*we parse the three MDS stat files and sum values*/
738 if( stats_values(FILEPATH_MDS_SERVER_STATS,name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
741 *nb_sample=tmp_nb_sample;
745 *sum_square=tmp_sum_square;
748 if( stats_values(FILEPATH_MDS_SERVER_READPAGE_STATS,name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
751 *nb_sample += tmp_nb_sample;
755 *sum_square += tmp_sum_square;
758 if( stats_values(FILEPATH_MDS_SERVER_SETATTR_STATS,name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
761 *nb_sample += tmp_nb_sample;
765 *sum_square += tmp_sum_square;