1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2005 Cluster File Systems, Inc.
5 * Author: PJ Kirner <pjkirner@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * include important headers
27 #include <net-snmp/net-snmp-config.h>
28 #include <net-snmp/net-snmp-includes.h>
29 #include <net-snmp/agent/net-snmp-agent-includes.h>
35 #include <sys/types.h>
36 #if defined (__linux__)
45 #include "lustre-snmp-util.h"
47 /*********************************************************************
48 * Function: get_file_list
50 * Description: For the given valid directory path, returns the list
51 * all directories or files in that path.
53 * Input: 'dirname' the directory path.
54 * 'file_type' if this takes the value DIR_TYPE then
55 * returns the list of directories in that path.
56 * If its of type FILE_TYPE then returns the list of files
58 * 'count' pointer to number of elements returned in the
61 * Output: List of directories/files in that path.
63 *********************************************************************/
65 char *get_file_list(const char *dirname, int file_type, uint32_t *count)
69 struct dirent *pdirent = NULL;
74 char filename[MAX_PATH_SIZE];
77 if ((dirname == NULL) || ((pdir = opendir(dirname)) == NULL )) {
78 if (dirname == NULL) {
79 report("%s %s:line %d %s", __FILE__, __FUNCTION__, __LINE__,
80 "NULL directory is passed as parameter to funtion");
82 report("%s %s:line %d Error in opening the dir %s", __FILE__,
83 __FUNCTION__, __LINE__, dirname);
91 if ((pdirent = readdir(pdir)) == NULL)
94 /* Skip over '.' and '..' directores */
95 if ((pdirent->d_name[0] == '.') ||
96 !strcmp(pdirent->d_name, FILENAME_NUM_REF))
99 sprintf(filename, "%s/%s", dirname, pdirent->d_name);
100 cond1 = (file_type == FILE_TYPE) && is_directory(filename);
101 cond2 = (file_type == DIR_TYPE) && (!is_directory(filename));
106 /* Calculate the number of bytes for this new entry.*/
107 byte_count += strlen(pdirent->d_name) + 1;
113 if (file_count != 0) {
115 /* need one extra one for the finall NULL terminator*/
116 if ((ret_str = (char *) malloc(byte_count + 1)) == NULL) {
117 report("get_file_list() failed to malloc(%d)",byte_count+1);
124 while (file_count != 0) {
125 if ((pdirent = readdir(pdir)) == NULL)
128 if ((pdirent->d_name[0] == '.') ||
129 !strcmp(pdirent->d_name, FILENAME_NUM_REF))
132 sprintf(filename, "%s/%s", dirname, pdirent->d_name);
133 cond1 = (file_type == FILE_TYPE) && is_directory(filename);
134 cond2 = (file_type == DIR_TYPE) && (!is_directory(filename));
139 strcpy(ret_str + curr_offset, pdirent->d_name);
140 curr_offset = curr_offset + strlen(pdirent->d_name) + 1;
143 /* Put in the finall null terminator*/
144 ret_str[byte_count] = '\0';
151 /*********************************************************************
152 * Function: is_directory
154 * Description: Checks if given filename is a directory or not.
155 * all directories or files in that path.
157 * Input: 'filename' the directory path to be checked.
159 * Output: Returns 1 if its a directory else 0.
161 *********************************************************************/
163 int is_directory(const char *filename)
169 result = stat(filename, &statf);
170 return ((result == SUCCESS) && (statf.st_mode & S_IFDIR));
173 /*********************************************************************
174 * Function: read_string
176 * Description: For the given valid file path, reads the data in
179 * Input: 'filepath' the file whose data is to be accessed.
180 * 'lustre_var' the data from the file is read into
181 * this variable, returned to the requestor.
182 * 'var_max_size' the max size of the string
183 * 'report_error' boolean if error should be reported on
186 * Output: Returns SUCCESS if read successfully from file else
188 *********************************************************************/
190 int read_string(const char *filepath, char *lustre_var, size_t var_max_size)
194 int ret_val = SUCCESS;
195 int report_error = 1;
197 if ((filepath == NULL) || (lustre_var == NULL)) {
198 report("%s %s:line %d %s", __FILE__, __FUNCTION__, __LINE__,
199 "Input parameter is NULL");
202 fptr = fopen(filepath, "r");
206 report("%s %s:line %d Unable to open the file %s", __FILE__,
207 __FUNCTION__, __LINE__, filepath);
210 if (fgets(lustre_var, var_max_size, fptr) == NULL) {
211 report("%s %s:line %d read failed for file %s", __FILE__,
212 __FUNCTION__, __LINE__, filepath);
215 len = strlen(lustre_var);
217 Last char is EOF, before string ends,
218 so '\0' is moved to last but one.
220 lustre_var[len-1] = lustre_var[len];
228 /**************************************************************************
229 * Function: lustrefs_ctrl
231 * Description: Execute /etc/init.d/lustre script for starting,
232 * stopping and restarting Lustre services in child process.
234 * Input: Start/Stop/Restart Command Number.
235 * Output: Returns void
237 **************************************************************************/
239 void lustrefs_ctrl(int command)
243 cmd[0] = LUSTRE_SERVICE;
262 report("failed to execvp(\'%s %s\')",cmd[0],cmd[1]);
267 /*****************************************************************************
268 * Function: get_sysstatus
270 * Description: Read /var/lustre/sysStatus file, and based on file contents
271 * return the status of Lustre services.
274 * Output: Return ONLINE/OFFLINE/ONLINE PENDING/OFFLINE PENDING status
277 ****************************************************************************/
279 int get_sysstatus(void)
283 int ret_val = ERROR ;
284 char sys_status[50] = {0};
286 if(SUCCESS == read_string(FILENAME_SYS_STATUS,sys_status,sizeof(sys_status)))
288 if (memcmp(sys_status, STR_ONLINE_PENDING,strlen(STR_ONLINE_PENDING)) == 0)
289 ret_val = ONLINE_PENDING;
290 else if (memcmp(sys_status, STR_ONLINE, strlen(STR_ONLINE)) == 0)
292 else if (memcmp(sys_status, STR_OFFLINE_PENDING,strlen(STR_OFFLINE_PENDING)) == 0)
293 ret_val = OFFLINE_PENDING;
294 else if (memcmp(sys_status, STR_OFFLINE, strlen(STR_OFFLINE)) == 0)
297 report("%s %s:line %d Bad Contents in file %s \'%s\'", __FILE__,
298 __FUNCTION__, __LINE__, FILENAME_SYS_STATUS,sys_status);
304 /*****************************************************************************
305 * Function: read_ulong
307 * Description: Read long values from lproc and copy to the location
308 * pointed by input parameter.
310 * Input: file path, and pointer for data to be copied
312 * Output: Return ERROR or SUCCESS.
314 ****************************************************************************/
316 int read_ulong(const char *file_path, unsigned long *valuep)
318 char file_data[MAX_LINE_SIZE];
321 if ((ret_val = read_string(file_path, file_data,sizeof(file_data))) == SUCCESS){
322 *valuep = strtoul(file_data,NULL,10);
327 /*****************************************************************************
328 * Function: read_counter64
330 * Description: Read counter64 values from lproc and copy to the location
331 * pointed by input parameter.
333 * Input: file path, and pointer for data to be copied
335 * Output: Return ERROR or SUCCESS.
337 ****************************************************************************/
339 int read_counter64(const char *file_path, counter64 *c64,int factor)
341 char file_data[MAX_LINE_SIZE];
343 unsigned long long tmp = 0;
345 if ((ret_val = read_string(file_path, file_data,sizeof(file_data))) == SUCCESS) {
346 tmp = atoll(file_data) * factor;
347 c64->low = (unsigned long) (0x0FFFFFFFF & tmp);
348 tmp >>= 32; /* Shift right by 4 bytes */
349 c64->high = (unsigned long) (0x0FFFFFFFF & tmp);
354 /*****************************************************************************
355 * Function: get_nth_entry_from_list
357 * Description: Find the n'th entry from a null terminated list of string
359 * Input: dir_list - the list
360 * num - the number of elements in the list
361 * index - the index we are looking for
363 * Output: Return NULL on failure, or the string name on success.
365 ****************************************************************************/
367 const char *get_nth_entry_from_list(const char* dir_list,int num,int index)
374 * if we've reached the end of the list for some reason
375 * because num was wrong then stop processing
377 if( *(dir_list+cur_ptr) == 0)
380 /* If we've found the right one */
382 return dir_list+cur_ptr;
384 /* Move to the next one*/
385 cur_ptr += strlen(dir_list + cur_ptr)+1;
390 /*****************************************************************************
393 * Description: This function used to report error msg to stderr and log into
394 * log file(default file:/var/log/snmpd.log) when agent is started with
395 * debug option -Dlsnmpd
396 * Input: format string and variable arguments.
398 ****************************************************************************/
400 void report(const char *fmt, ...)
405 va_start(arg_list, fmt);
406 vsprintf(buf, fmt, arg_list);
409 DEBUGMSGTL(("lsnmpd", "%s\n", buf));
410 fprintf(stderr, "%s\n", buf);
416 /**************************************************************************
417 * Function: oid_table_ulong_handler
419 * Description: Fetch a unsigned long from the given location.
420 * Setup var_len, and return a pointer to the data.
422 * Input: file_path, and var_len pointer
424 * Output: NULL on failure, or pointer to data
426 **************************************************************************/
429 oid_table_ulong_handler(
430 const char* file_path,
433 static unsigned long ulong_ret;
434 if (SUCCESS != read_ulong(file_path,&ulong_ret))
436 *var_len = sizeof(ulong_ret);
437 return (unsigned char *) &ulong_ret;
440 /**************************************************************************
441 * Function: oid_table_c64_handler
443 * Description: Fetch a counter64 from the given location.
444 * Setup var_len, and return a pointer to the data.
446 * Input: file_path, and var_len pointer
448 * Output: NULL on failure, or pointer to data
450 **************************************************************************/
452 unsigned char* oid_table_c64_handler(const char* file_path,size_t *var_len)
454 static counter64 c64;
455 if (SUCCESS != read_counter64(file_path,&c64,1))
457 *var_len = sizeof(c64);
458 return (unsigned char *) &c64;
461 /**************************************************************************
462 * Function: oid_table_c64_kb_handler
464 * Description: Fetch a counter64 from the given location.
465 * Setup var_len, and return a pointer to the data.
466 * Different than oid_table_c64_handler in that
467 * the original value is multiplied by 1024 before converting
468 * to a counter64. (e.g. turn KB into a Byte scaled value)
470 * Input: file_path, and var_len pointer
472 * Output: NULL on failure, or pointer to data
474 **************************************************************************/
476 unsigned char* oid_table_c64_kb_handler(const char* file_path,size_t *var_len)
478 static counter64 c64;
479 /* scale by factor of 1024*/
480 if (SUCCESS != read_counter64(file_path,&c64,1024))
482 *var_len = sizeof(c64);
483 return (unsigned char *) &c64;
486 /**************************************************************************
487 * Function: oid_table_obj_name_handler
489 * Description: Just copy the file_path and return as the output value.
491 * Input: file_path, and var_len pointer
493 * Output: NULL on failure, or pointer to data
495 **************************************************************************/
498 oid_table_obj_name_handler(
499 const char* file_path,
502 static unsigned char string[SPRINT_MAX_LEN];
503 *var_len = strlen(file_path);
504 *var_len = MIN_LEN(*var_len, sizeof(string));
505 memcpy(string, file_path, *var_len);
506 return (unsigned char *) string;
509 /**************************************************************************
510 * Function: oid_table_string_handler
512 * Description: Fetch a string from the given location.
513 * Setup var_len, and return a pointer to the data.
515 * Input: file_path, and var_len pointer
517 * Output: NULL on failure, or pointer to data
519 **************************************************************************/
522 oid_table_string_handler(
523 const char* file_path,
526 static unsigned char string[SPRINT_MAX_LEN];
527 if( SUCCESS != read_string(file_path, string,sizeof(string)))
529 *var_len = strlen(string);
530 return (unsigned char *) string;
534 /**************************************************************************
535 * Function: oid_table_is_directory_handler
537 * Description: Determine if the file_path is a directory.
538 * Setup a boolean return value.
539 * Setup var_len, and return a pointer to the data.
541 * Input: file_path, and var_len pointer
543 * Output: NULL on failure, or pointer to data
545 **************************************************************************/
548 oid_table_is_directory_handler(
549 const char* file_path,
552 static long long_ret;
553 long_ret = is_directory(file_path);
554 *var_len = sizeof(long_ret);
555 return (unsigned char *) &long_ret;
558 /**************************************************************************
559 * Function: var_genericTable
561 * Description: Handle Table driven OID processing
563 **************************************************************************/
566 var_genericTable(struct variable *vp,
571 WriteMethod **write_method,
573 struct oid_table *ptable)
578 unsigned char *ret_val = NULL;
580 const char* obj_name;
584 * Get the list of file. If there are no elements
587 if( 0 == (dir_list = get_file_list(path, DIR_TYPE, &num)))
593 if (header_simple_table(vp,name,length,exact,var_len,write_method, num)
595 goto cleanup_and_exit;
598 * The number of the device we're looking at
600 deviceindex = name[*length - 1] - 1;
603 * If we couldn't find this element
604 * something must have recently changed return
607 if(deviceindex >= num){
608 report("deviceindex=%d exceeds number of elements=%d",deviceindex,num);
609 goto cleanup_and_exit;
613 * Fetch the object name from the list
615 obj_name = get_nth_entry_from_list(dir_list,num,deviceindex);
616 if(obj_name == NULL){
618 * Note this should never really happen because we check deviceindex >=num
619 * above. And dir_list should be consitent with num
620 * but just in case...
622 report("object name not found in list",deviceindex,num);
623 goto cleanup_and_exit;
627 * Find the matching magic - or the end of the list
629 while(ptable[i].magic != vp->magic && ptable[i].magic != 0)
633 * If we didn't find a matching entry return
635 if(ptable[i].magic==0)
636 goto cleanup_and_exit;
639 * If the name is NULL is a special case and
640 * just just pass the obj_name as the file_path
641 * otherwise we create a file path from the given components
643 if(ptable[i].name != 0){
644 char file_path[MAX_PATH_SIZE];
645 sprintf(file_path, "%s%s/%s",path,obj_name,ptable[i].name);
646 ret_val = ptable[i].fhandler(file_path,var_len);
649 ret_val = ptable[i].fhandler(obj_name,var_len);
656 /**************************************************************************
657 * Function: stats_values
659 * Description: Setup nb_sample, min, max, sum and sum_square stats values
660 for name_value from filepath.
662 * Input: filepath, name_value,
663 * pointer to nb_sample, min, max, sum, sum_square
665 * Output: SUCCESS or ERROR on failure
667 **************************************************************************/
668 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)
671 char line[MAX_LINE_SIZE];
672 int nbReadValues = 0;
674 if( (statfile=fopen(filepath,"r")) == NULL) {
675 report("stats_value() failed to open %s",filepath);
678 /*find the good line for name_value*/
680 if( fgets(line,MAX_LINE_SIZE,statfile) == NULL ) {
681 report("stats_values() failed to find %s values in %s stat_file",name_value,statfile);
684 } while ( strstr(line,name_value) == NULL );
686 if((nbReadValues=sscanf(line,"%*s %llu %*s %*s %llu %llu %llu %llu",nb_sample,min,max,sum,sum_square)) == 5) {
688 } else if( nbReadValues == 1 && *nb_sample == 0) {
689 *min = *max = *sum = *sum_square = 0;
692 report("stats_values() failed to read stats_values for %s value in %s stat_file",name_value,statfile);
704 /**************************************************************************
705 * Function: mds_stats_values
707 * Description: Setup nb_sample, min, max, sum and sum_square stats values
708 for mds stats name_value .
711 * pointer to nb_sample, min, max, sum, sum_square
713 * Output: SUCCESS or ERROR on failure
715 **************************************************************************/
716 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)
718 unsigned long long tmp_nb_sample=0,tmp_min=0,tmp_max=0,tmp_sum=0,tmp_sum_square=0;
719 /*we parse the three MDS stat files and sum values*/
720 if( stats_values(FILEPATH_MDS_SERVER_STATS,name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
723 *nb_sample=tmp_nb_sample;
727 *sum_square=tmp_sum_square;
730 if( stats_values(FILEPATH_MDS_SERVER_READPAGE_STATS,name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
733 *nb_sample += tmp_nb_sample;
737 *sum_square += tmp_sum_square;
740 if( stats_values(FILEPATH_MDS_SERVER_SETATTR_STATS,name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
743 *nb_sample += tmp_nb_sample;
747 *sum_square += tmp_sum_square;