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>
42 #include "lustre-snmp-util.h"
44 /**************************************************************************
46 *************************************************************************/
48 #define DEFAULT_POLL_INTERVAL_SECONDS 60
49 #define POLL_INTERVAL_ENV_VAR "LSNMP_POLL_INTERVAL"
50 #define SNMP_HEALTH_CHECK_TEST_FILE "LSNMP_HEALTH_CHECK_TEST_FILE"
52 /**************************************************************************
54 *************************************************************************/
56 static oid objid_snmptrap[] =
57 { 1,3,6,1,6,3,1,1,4,1,0};
58 static oid lustre_portals_trap[] =
59 { 1,3,6,1,4,1,13140,2,1,0,1};
60 static oid lustre_portals_trap_string[]=
61 { 1,3,6,1,4,1,13140,2,1,0,2};
62 static oid lustre_unhealthy_trap[] =
63 { 1,3,6,1,4,1,13140,2,1,0,3};
64 static oid lustre_unhealthy_trap_device_name_string[]=
65 { 1,3,6,1,4,1,13140,2,1,0,4};
66 static oid lustre_unhealthy_trap_reason_string[]=
67 { 1,3,6,1,4,1,13140,2,1,0,5};
69 /**************************************************************************
71 *************************************************************************/
73 typedef struct obd_unhealthy_entry_struct{
75 /*1-if seen as part of the the is_unhealthy scan, otherwise 0*/
78 /*single linked list pointer*/
79 struct obd_unhealthy_entry_struct *next;
81 /*obdname - variable size*/
86 /**************************************************************************
88 *************************************************************************/
90 int get_poll_interval_seconds();
91 void health_poll_worker(unsigned int registration_number, void *clientarg);
92 void send_portals_catastrophe_trap(char *reason_string);
93 void send_obd_unhealthy_trap(char *obd_name,char *reason_string);
94 int is_obd_newly_unhealthy(const char* obd_name);
95 void obd_unhealthy_scan(void);
96 void health_entry_parser(void);
98 /**************************************************************************
100 *************************************************************************/
102 static int g_sent_portals_catastrophe = 0;
103 static obd_unhealthy_entry* g_obd_unhealthy_list = NULL;
104 static int g_poll_interval_seconds;
105 static unsigned int g_registration_handle;
106 static char *g_health_check_test_file = 0;
108 /*****************************************************************************
109 * Function: initilize_trap_handler
111 * Description: Initlized the trap poll haalder.
115 * Output: Global g_poll_interval_seconds is set.
117 ****************************************************************************/
119 void initilize_trap_handler(void)
121 g_poll_interval_seconds = get_poll_interval_seconds();
123 g_registration_handle = snmp_alarm_register(g_poll_interval_seconds, 0, health_poll_worker, NULL);
124 if (g_registration_handle == 0)
125 report("%s %s: line %d %s", __FILE__, __FUNCTION__, __LINE__,
126 "snmp_alarm_register failed");
128 DEBUGMSGTL(("lsnmpd","lsnmp alarm registered poll interval = %d seconds\n",g_poll_interval_seconds));
130 g_health_check_test_file = getenv(SNMP_HEALTH_CHECK_TEST_FILE);
131 if(g_health_check_test_file != 0)
132 DEBUGMSGTL(("lsnmpd","lsnmp health check test file set to \'%s\'\n",g_health_check_test_file));
135 /*****************************************************************************
136 * Function: terminate_trap_handler
138 * Description: Terminate the trap poll haalder.
142 * Output: Global g_poll_interval_seconds is set.
144 ****************************************************************************/
146 void terminate_trap_handler(void)
148 snmp_alarm_unregister(g_registration_handle);
151 /*****************************************************************************
152 * Function: get_poll_interval_seconds
154 * Description: This function used to get the poll period for timer, which
155 * is used to read throughput values periodically.
157 * Output: Alarm period, default value(if env var not set) otherwise.
158 ****************************************************************************/
160 int get_poll_interval_seconds()
163 int ret_val = DEFAULT_POLL_INTERVAL_SECONDS;
165 /* Get Alarm period for reading the Lustre client table. */
167 alarm_period = getenv(POLL_INTERVAL_ENV_VAR);
168 if (alarm_period != NULL) {
169 char *ptr = alarm_period;
170 while(isdigit(*ptr)) ptr++;
172 /* if we have only digits then conver it*/
174 int time = atoi(alarm_period);
176 ret_val = time; /* Alarm period in seconds */
182 /*****************************************************************************
183 * Function: health_poll_worker
185 * Description: This is the routine registered to system timer for updating
186 * the throughput values for all the clients and its respective osc(s).
188 * Input: 'registration_number` value obtained during the alarm registration
189 * 'clientarg' pointing to user defined data type.
191 *****************************************************************************/
193 void health_poll_worker(unsigned int registration_number, void *clientarg)
195 health_entry_parser();
197 /* Register the function again to call after lustre_alarm_period */
198 if (!snmp_alarm_register(g_poll_interval_seconds, 0, health_poll_worker, NULL)) {
199 report("%s %s:line %d %s", __FILE__, __FUNCTION__, __LINE__,
200 "snmp_alarm_register failed");
204 /*****************************************************************************
205 * Function: health_entry_parser
207 * Description: This routine is called to parse the health_check entry
211 *****************************************************************************/
213 void health_entry_parser(void)
216 char string[MAX_LINE_SIZE];
217 int b_seen_portals_catastrophe = 0;
218 const char *filename = g_health_check_test_file == 0 ?
219 LUSTRE_PATH FILENAME_SYSHEALTHCHECK :
220 g_health_check_test_file;
222 /*DEBUGMSGTL(("lsnmpd","health_entry_parser(%s)\n",filename));*/
224 /* Open the file. Use the test file env variable if
226 fptr = fopen(filename,"r");
228 /* If the path is not found do nothing */
232 while( NULL != fgets(string, sizeof(string), fptr)){
234 /*DEBUGMSGTL(("lsnmpd","health_entry_parser() looking at = \'%s\'\n",string));*/
237 * First handle the portals catastrophe
238 * Look for the string "LBUG"
240 if(0 == strncmp(string,"LBUG",4)){
242 * If we haven't sent the catastrophe message yet
243 * send it now. And keep track that we've sent it
245 if(!g_sent_portals_catastrophe){
246 send_portals_catastrophe_trap("LBUG");
247 g_sent_portals_catastrophe = 1;
249 b_seen_portals_catastrophe = 1;
253 * Now handle any of the OBD object failures
254 * look for "device <OBDNAME> reported unhealthy"
256 else if(0 == strncmp(string,"device ",7)){
257 char *obd_name = string+7;
258 char *space_after_obd_name;
261 * Now find the space after the obd name
262 * Again if there is no space we're in trouble
264 space_after_obd_name = strchr(obd_name,' ');
265 if(space_after_obd_name == 0)
269 * Null terminate the obd_name
271 *space_after_obd_name = 0;
273 DEBUGMSGTL(("lsnmpd","Looking at obd=%s\n",obd_name));
276 * If we haven't sent a trap for this one
279 if(is_obd_newly_unhealthy(obd_name))
280 send_obd_unhealthy_trap(obd_name,"unhealthy");
284 /* If we don't find it reset the catastrope flag*/
285 if(!b_seen_portals_catastrophe && g_sent_portals_catastrophe)
287 DEBUGMSGTL(("lsnmpd","LBUG has been cleared\n"));
288 g_sent_portals_catastrophe = 0;
292 * Any <OBDNAMES> that weren't queried above are now unhealthy.
293 * Scan through and cleanup the newly healthy obds
295 obd_unhealthy_scan();
300 /*****************************************************************************
301 * Function: send_portals_catastrophe_trap
303 * Description: Send the SNMP V2 trap
305 * Input: 'reason_string' the reason for the catastrope.
308 *****************************************************************************/
310 void send_portals_catastrophe_trap(char *reason_string)
313 * Setup the trap variables.
314 * It's a linked list of netsnmp_variable_list items.
316 netsnmp_variable_list var_trap[2];
318 DEBUGMSGTL(("lsnmpd","Sending portals catastrophe trap reason=%s\n",reason_string));
321 * Setup the first variable in the trap data.
322 * Have it chain to another variable.
324 var_trap[0].next_variable = &var_trap[1];
326 /*The "name" must be the standard snmp "trap" OID.*/
327 var_trap[0].name = objid_snmptrap;
328 var_trap[0].name_length = sizeof(objid_snmptrap) / sizeof(oid);
330 /*But the data contained in this variable, is an OID that is the trap OID.*/
331 var_trap[0].type = ASN_OBJECT_ID;
332 var_trap[0].val.objid = lustre_portals_trap;
333 var_trap[0].val_len = sizeof(lustre_portals_trap);
336 * Setup the second variable in the trap data.
337 * It is the last in the chain so set next to NULL
339 var_trap[1].next_variable = NULL;
341 /*The "name" is the OID of the portals trap reason strong*/
342 var_trap[1].name = lustre_portals_trap_string;
343 var_trap[1].name_length = sizeof(lustre_portals_trap_string) / sizeof(oid);
345 /*And the data is a octet string, that contains the actually reason string*/
346 var_trap[1].type = ASN_OCTET_STR;
347 var_trap[1].val.string = reason_string;
348 var_trap[1].val_len = strlen(reason_string);
350 /*And now send off the trap*/
351 send_v2trap(var_trap);
355 /*****************************************************************************
356 * Function: send_obd_unhealthy_trap
358 * Description: Send the SNMP V2 trap
360 * Input: 'obd_name' the name of the obd
361 * 'reason_string' the reason for the catastrope.
363 *****************************************************************************/
365 void send_obd_unhealthy_trap(char *obd_name,char *reason_string)
368 * Setup the trap variables.
369 * It's a linked list of netsnmp_variable_list items.
371 netsnmp_variable_list var_trap[3];
373 DEBUGMSGTL(("lsnmpd","Sending OBD unhealthy trap obd=%s reason=%s\n",obd_name,reason_string));
376 * Setup the first variable in the trap data.
377 * Have it chain to another variable.
379 var_trap[0].next_variable = &var_trap[1];
381 /*The "name" must be the standard snmp "trap" OID.*/
382 var_trap[0].name = objid_snmptrap;
383 var_trap[0].name_length = sizeof(objid_snmptrap) / sizeof(oid);
385 /*But the data contained in this variable, is an OID that is the trap OID.*/
386 var_trap[0].type = ASN_OBJECT_ID;
387 var_trap[0].val.objid = lustre_unhealthy_trap;
388 var_trap[0].val_len = sizeof(lustre_unhealthy_trap);
391 * Setup the second variable in the trap data.
392 * Have it chain to another variable.
394 var_trap[1].next_variable = &var_trap[2];;
396 /*The "name" is the OID of the portals trap reason strong*/
397 var_trap[1].name = lustre_unhealthy_trap_device_name_string;
398 var_trap[1].name_length = sizeof(lustre_unhealthy_trap_device_name_string) / sizeof(oid);
400 /*And the data is a octet string, that contains the actually reason strong*/
401 var_trap[1].type = ASN_OCTET_STR;
402 var_trap[1].val.string = obd_name;
403 var_trap[1].val_len = strlen(obd_name);
406 * Setup the third variable in the trap data.
407 * It is the last in the chain so set next to NULL
409 var_trap[2].next_variable = NULL;
411 /*The "name" is the OID of the portals trap reason strong*/
412 var_trap[2].name = lustre_unhealthy_trap_reason_string;
413 var_trap[2].name_length = sizeof(lustre_unhealthy_trap_reason_string) / sizeof(oid);
415 /*And the data is a octet string, that contains the actually reason strong*/
416 var_trap[2].type = ASN_OCTET_STR;
417 var_trap[2].val.string = reason_string;
418 var_trap[2].val_len = strlen(reason_string);
420 /*And now send off the trap*/
421 send_v2trap(var_trap);
425 /*****************************************************************************
426 * Function: is_obd_newly_unhealthy
428 * Description: Deterime if the obd is going from health->unhealth
429 * Also mark all unhealhy (new and old) as seen.
431 * Input: 'obd_name' the name of the obd
433 * Output: 1 if newly unhealthy 0 if previolsy unhealthy
434 *****************************************************************************/
436 int is_obd_newly_unhealthy(const char* obd_name)
438 /*for all elements in g_obd_unhealthy_list*/
439 obd_unhealthy_entry* walker;
440 obd_unhealthy_entry* entry;
443 for(walker = g_obd_unhealthy_list; walker != 0; walker = walker->next)
445 /*If the names match*/
446 if(0 == strcmp (walker->name,obd_name))
448 /* Commented out because it was just to noisy!
449 * DEBUGMSGTL(("lsnmpd","obd %s was already unhealthy\n",obd_name));
452 /*Mark the entry as seen, and return that it was previously unhealthy*/
458 DEBUGMSGTL(("lsnmpd","obd %s is now unhealthy\n",obd_name));
460 /*We didn't find an entry so we need to create a new one. */
461 /*Calculate the obd_name length*/
462 name_len = strlen(obd_name)+1;
464 /*Allocate a new entry*/
465 entry = malloc(sizeof(*entry) + name_len);
467 /*Put this element at the front of the list*/
468 entry->next = g_obd_unhealthy_list;
469 g_obd_unhealthy_list = entry;
471 /*Mark it initially as seen*/
474 /*And copy the entry name*/
475 memcpy(entry->name,obd_name,name_len);
477 /*return this obd as newly unhealthy.*/
482 /*****************************************************************************
483 * Function: obd_unhealthy_scan
485 * Description: Deterime if any obd is going from unhealthy->healthy
486 * Any of the obds that weren't "seen" by the
487 * is_obd_newly_unhealthy() pass are now health so
488 * remove them from the lists
489 * Also clear all "seen" flags.
493 *****************************************************************************/
495 void obd_unhealthy_scan(void)
497 /*fore all elements in g_obd_unhealthy_list*/
498 obd_unhealthy_entry* walker = g_obd_unhealthy_list;
499 obd_unhealthy_entry* prev = 0;
502 /*remove any that was not seen as unhealthy the last time*/
503 if(walker->seen == 0)
505 /*Remove element from the list, but first fix up the walker pointer*/
506 obd_unhealthy_entry* temp = walker;
508 DEBUGMSGTL(("lsnmpd","obd %s is now healthy\n",walker->name));
510 walker = walker->next;
512 /*Now adjust the pointers to effectively remove this entry*/
514 g_obd_unhealthy_list = walker;
518 /*And free the pointer. */
520 /*walker and prev are correctly setup so we can go around the loop again.*/
523 /*Mark all other entries as NOT seen for next pass through*/
527 /*Go onto the next entry*/
529 walker = walker->next;