#include "nagios_common_snmp.h" #define SCRIPT_VERSION 0.5 #define TMP_FILE_NAME_PREFIX "tmp_Nagios_proc." /* default delta for averages : 300s = 5min */ #define DEFAULT_DELTA_AVERAGE 300 /******************************** help & usage ********************************/ char usage[]= ""; /******************************** SNMP data ********************************/ oid ThermalCondition_table[]= {1,3,6,1,4,1,232,6,2,6,1}; oid ThermalAction_table[]= {1,3,6,1,4,1,232,6,2,6,2}; oid ThermalStatus_table[]= {1,3,6,1,4,1,232,6,2,6,3}; oid ThermalFan_table[]= {1,3,6,1,4,1,232,6,2,6,4}; oid ThermalCPUFan_table[]= {1,3,6,1,4,1,232,6,2,6,5}; oid ThermalFanTable_table[]= {1,3,6,1,4,1,232,6,2,6,7,1,9}; void print_usage() { printf("Usage: %s %s %s\n",plugin_name, common_usage, usage); exit(N_UNKNOWN); } void print_version() { printf("%s version %.2f / Package version %.2f\n",plugin_name,SCRIPT_VERSION,NAGIOS_SNMP_VERSION); exit(N_UNKNOWN); } void print_help(){ printf(_("\nSNMP Process Monitor for Nagios version %.2f\n"),SCRIPT_VERSION); printf(_("Nagios SNMP plugins version %.2f, GPL licence, (c)2004-2007 Patrick Proy\n\n"),NAGIOS_SNMP_VERSION); printf("Usage: %s %s %s\n",plugin_name, common_usage, usage); DEFAULT_HELP_PRINT exit(N_UNKNOWN); } /* process struct to put snmp data for each process */ struct process_struct { int index; int state; int mem_used; int cpu_count; }; /******************************** Main of check_snmp_process ********************************/ int main(int argc, char ** argv) { /* limits */ int n_maxOID=500; /* OID array */ oid valOID[n_maxOID][MAX_OID_LEN]; size_t valOID_len[n_maxOID]; int n_valOID; int index_match,index2_match; snmp_table *proc_index[n_maxOID]; /* result tables */ snmp_table *descT,*tempT,*valueT,*paramT,*temp2T, *temp3T; /* ThermalCondition_table, ThermalAction_table, ThermalStatus_table, ThermalFan_table, ThermalCPUFan_table, ThermalFanTable_table */ snmp_table *thermalConditionT, *thermalActionT, *thermalStatusT, *thermalFanT, *thermalCPUFanT, *thermalFanTableT; /* counters, output, etc... */ int i,j; char ThCondition[100]="",ThAction[100]="",ThSensor[100]=""; char FanCondition[100]="",CpuFan[100]="",otherFan[100]=""; char *buff_select; int num_proc,test_int,num_proc_ok; int final_status; float res_memory; int res_cpu; /* file datas & name */ unsigned long long int file_values[MAX_ROWS][MAX_ITEMS]; /* to get & store data in file */ int rows=0;int items=0;int file_ret,found_data; char base_file_name[MAX_FILE_LENGTH]=BASE_DIR; char file_name[MAX_FILE_LENGTH]; long unsigned int timenow,trigger,trigger_low; float found_value; /***************** options init ********************/ options_nagsnmp liste[] = { COMMON_OPTIONS /*********** Specific for the script ***************/ /* { type ,short , o_long , present,ivalue, cvalue, int_value n_array} */ }; int n_liste=COMOPTNUM+0; /* Initialise gettext and other parameters */ if (init_plugins_gettext()!=0) { printf("Cannot initialize GETTEXT\n"); return N_UNKNOWN;} strcpy(plugin_name,"check_snmp_hp_fan"); /* Plugin name for cache */ strcpy(plugin_tmp_prefix,TMP_FILE_NAME_PREFIX); /***************** options init ********************/ init_options(); if (get_options (argc, argv,liste,n_liste)!=0) print_usage(); i=check_common_options(); if (i==1) print_usage(); else if (i==2) print_help(); else if (i==3) print_version() ; /***************** check options *****************/ /**************** open snmp session ******************/ if (init_snmp_session()!=0) { printf(_("Error opening SNMP session : %s\n"),error); free(error); return N_UNKNOWN; } /***************** Get name or path table *****************/ thermalConditionT=get_snmp_table (ss,ThermalCondition_table, sizeof (ThermalCondition_table) / sizeof (oid),snmp_bulk,&error); if (thermalConditionT==NULL) { printf(_("Error reading table ThermalCondition_table: %s\n"),error); free(error); return N_UNKNOWN; } thermalFanT=get_snmp_table (ss,ThermalFan_table, sizeof (ThermalFan_table) / sizeof (oid),snmp_bulk,&error); if (thermalFanT==NULL) { printf(_("Error reading table : %s\n"),error); free(error); return N_UNKNOWN; } /**************** parse results ******************/ tempT=thermalConditionT; while(tempT!=NULL) { int my_thermalcondition; index_match=(int) tempT->name[tempT->name_length-1]; if (get_value_int(ThermalCondition_table,sizeof (ThermalCondition_table)/sizeof (oid),index_match,thermalConditionT,&my_thermalcondition)!=0) { printf("Error retrieving thermal condition"); break; } if (o_verb) {printf ("OID : "); for (i=0;iname_length;i++) printf(".%lu",tempT->name[i]); printf(" : %i\n", my_thermalcondition);} if(my_thermalcondition==1) { final_status = max_state (final_status, N_UNKNOWN); sprintf(ThCondition,_("Thermal Conditions: UNKNOWN")); } if(my_thermalcondition==2) { final_status = max_state (final_status, N_OK); sprintf(ThCondition,_("Thermal Conditions: OK")); } if(my_thermalcondition==3) { final_status = max_state (final_status, N_WARNING); sprintf(ThCondition,_("Thermal Conditions: DEGRADED")); } if(my_thermalcondition==4) { final_status = max_state (final_status, N_CRITICAL); sprintf(ThCondition,_("Thermal Conditions: CRITICAL")); } if(my_thermalcondition!=2) { int my_sensor; int my_action; // Get some more information thermalActionT=get_snmp_table (ss,ThermalAction_table, sizeof (ThermalAction_table) / sizeof (oid),snmp_bulk,&error); temp2T=thermalActionT; if (thermalActionT==NULL) { printf(_("Error reading table : %s\n"),error); free(error); return N_UNKNOWN; } thermalStatusT=get_snmp_table (ss,ThermalStatus_table, sizeof (ThermalStatus_table) / sizeof (oid),snmp_bulk,&error); temp3T=thermalStatusT; if (thermalStatusT==NULL) { printf(_("Error reading table : %s\n"),error); free(error); return N_UNKNOWN; } // Check sensor condition if (o_verb) {printf ("OID : "); for (i=0;iname_length;i++) printf(".%lu",temp2T->name[i]); } if (get_value_int(ThermalStatus_table,sizeof (ThermalStatus_table)/sizeof (oid),index_match,thermalStatusT,&my_sensor)!=0) { printf("Error retrieving thermal sensor condition"); break; } if (o_verb) {printf ("OID : "); for (i=0;iname_length;i++) printf(".%lu",temp3T->name[i]); } // Proposed action if (get_value_int(ThermalAction_table,sizeof (ThermalAction_table)/sizeof (oid),index_match,thermalActionT,&my_action)!=0) { printf("Error retrieving thermal action"); break; } if(my_sensor==1) sprintf(ThSensor,_("[Sensor:UNK]")); if(my_sensor==2) sprintf(ThSensor,_("[Sensor:OK]")); if(my_sensor==3) sprintf(ThSensor,_("[Sensor:DEGRADED]")); if(my_sensor==4) sprintf(ThSensor,_("[Sensor:FAILED]")); if(my_action==1) sprintf(ThAction,_("[Action:UNK]")); if(my_action==2) sprintf(ThAction,_("[Action:CONTINUE]")); if(my_action==3) sprintf(ThAction,_("[Action:SHUTDOWN]")); } tempT=tempT->next; } tempT=thermalFanT; while(tempT!=NULL) { int my_fan; index_match=(int) tempT->name[tempT->name_length-1]; if (get_value_int(ThermalFan_table,sizeof (ThermalFan_table)/sizeof (oid),index_match,thermalFanT,&my_fan)!=0) { printf("Error retrieving fan condition"); break; } if (o_verb) {printf ("OID : "); for (i=0;iname_length;i++) printf(".%lu",tempT->name[i]); printf(" : %i\n", my_fan);} if(my_fan==1) { final_status = max_state (final_status, N_UNKNOWN); sprintf(FanCondition,_(",Fan Conditions: UNKNOWN")); } if(my_fan==2) { final_status = max_state (final_status, N_OK); sprintf(FanCondition,_(", Fan Conditions: OK")); } if(my_fan==3) { final_status = max_state (final_status, N_WARNING); sprintf(FanCondition,_(", Fan Conditions: DEGRADED")); } if(my_fan==4) { final_status = max_state (final_status, N_CRITICAL); sprintf(FanCondition,_(", Fan Conditions: CRITICAL")); } if(my_fan!=2) { int my_cpufan; thermalCPUFanT=get_snmp_table (ss,ThermalCPUFan_table, sizeof (ThermalCPUFan_table) / sizeof (oid),snmp_bulk,&error); if (thermalCPUFanT==NULL) { printf(_("Error reading table : %s\n"),error); free(error); return N_UNKNOWN; } thermalFanTableT=get_snmp_table (ss,ThermalFanTable_table, sizeof (ThermalFanTable_table) / sizeof (oid),snmp_bulk,&error); if (thermalFanTableT==NULL) { printf(_("Error reading table : %s\n"),error); free(error); return N_UNKNOWN; } temp2T=thermalCPUFanT; temp3T=thermalFanTableT; if (o_verb) {printf ("OID : "); for (i=0;iname_length;i++) printf(".%lu",temp2T->name[i]); printf ("\n");} if (get_value_int(ThermalCPUFan_table,sizeof (ThermalCPUFan_table)/sizeof (oid),index_match,thermalCPUFanT,&my_cpufan)!=0) { printf("Error retrieving cpu fan status"); break; } if(my_cpufan==1) sprintf(CpuFan,_("[CPU Fan: UNKNOWN]")); if(my_cpufan==2) sprintf(CpuFan,_("[CPU Fan: OK]")); if(my_cpufan==4) sprintf(CpuFan,_("[CPU Fan: FAILED]")); sprintf(otherFan,_("[Fans UNKNOWN]")); while(temp3T!=NULL){ int my_fans; index2_match=(int) temp3T->name[temp3T->name_length-1]; my_fans= temp3T->value_l; if (o_verb) {printf ("OID %i: ",index2_match); for (i=0;iname_length;i++) printf(".%lu",temp3T->name[i]); printf (": %i\n",my_fans);} if((my_fans==3)||(my_fans==4)) { sprintf(otherFan,_("[Fan id %i problem]"),index2_match); } temp3T=temp3T->next; } } tempT=tempT->next; } strcat_output(ThCondition); strcat_output(ThSensor); strcat_output(ThAction); strcat_output(FanCondition); strcat_output(CpuFan); strcat_output(otherFan); if (final_status==N_CRITICAL) { print_output_with_status(N_CRITICAL); return N_CRITICAL; } if (final_status==N_WARNING) { print_output_with_status(N_WARNING); return N_WARNING; } if (final_status==N_UNKNOWN) { print_output_with_status(N_UNKNOWN); return N_UNKNOWN; } print_output_with_status(N_OK); return N_OK; }