1 /*
2  * File:      check_cpq_thermal.c
3  * Copyright: (c) 2006 by Peter Gritsch
4  * Email:     s4nag@no-where.at
5  *
6  * This file is part of SNMP4Nagios.
7  *
8  * SNMP4Nagios is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or (at
11  * your option) any later version.
12  *
13  * SNMP4Nagios is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with SNMP4Nagios; if not, write to the
20  *
21  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22  * MA 02110-1301, USA
23  */
24 
25 #if HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #include "decoders.h"
30 #include "globals.h"
31 #include "nagiosif.h"
32 #include "snmpif.h"
33 #include "syslogif.h"
34 #include "strutils.h"
35 #include "utilities.h"
36 
37 
38 /* data type for storing scan results */
39 typedef struct
40 {
41     int v0;      /* found cpqHeThermalCondition */
42 } tScanRes;
43 
44 /* data type for storing data */
45 typedef struct
46 {
47     int   cond;  /* condition */
48 } tDataSet;
49 
50 
51 /*
52  * CPQHLTH-MIB::cpqHeThermalCondition.0 = .1.3.6.1.4.1.232.6.2.6.1.0
53  */
54 oid baseOID [] = { 1, 3, 6, 1, 4, 1, 232, 6, 2, 6, 1 };
55 size_t baseOIDLen = 11;
56 
57 /* scan results */
58 tScanRes* scanRes = NULL;
59 int scanResLen = 0;
60 
61 /* results of the SNMP queries */
62 tDataSet v;
63 
64 
SetGlobals()65 void SetGlobals ()
66 {
67     /* progName is set by ReadArgs() */
68     srvName = "check_cpq_ida";
69     progVersion = "0.8";
70     helpIndexP = NULL;
71     helpThresP = NULL;
72     helpPurpose = "Checks the status of a Compaq/HP IDA Controller.";
73     /* community is set by ReadArgs() */
74     /* commFile is set by ReadArgs() */
75     /* rrdStep is unused */
76     /* rrdHBeat is unused */
77     supportsPerfLog = FALSE;
78     hasIndexParam = FALSE;
79 }
80 
HelpSpecificParams()81 void HelpSpecificParams ()
82 {
83     /* No specific parameters */
84 }
85 
HelpExplain()86 void HelpExplain ()
87 {
88     printf (
89     "The plugin returns the overall thermal state of a Compaq server.\n"
90     "\n"
91     );
92 }
93 
HelpPerformanceData()94 void HelpPerformanceData ()
95 {
96     printf (
97     "There is no performance data provided.\n"
98     "\n"
99     );
100 }
101 
ChkOptSpecificParams(int argc,char ** argv,int * i)102 int ChkOptSpecificParams ( int argc, char** argv, int* i )
103 {
104     return FALSE;
105 }
106 
SnmpWalkHandleVariable(netsnmp_variable_list * aVar)107 void SnmpWalkHandleVariable ( netsnmp_variable_list* aVar )
108 {
109     if ( aVar->name_length == 12 )
110     {
111         if ( OidCmp ( aVar->name, baseOID, baseOIDLen ) == 0 )
112         {
113             int index;
114 
115             switch ( aVar->name [11] )
116             {
117                 case 0: /* cpqHeThermalCondition.0 */
118                     index = scanResLen;
119                     scanResLen++;
120                     scanRes = realloc ( scanRes,
121                             sizeof ( tScanRes ) * scanResLen );
122                     if ( scanRes == NULL ) ErrorMalloc ();
123                     scanRes [index].v0 = TRUE;
124                 break;
125                 default:
126                     /* don't care */
127                 break;
128             }
129         } else Log ( LOG_CRIT, "Unexpected OID while scanning services" );
130     } else Log ( LOG_CRIT, "Wrong length while scanning services" );
131 }
132 
ScanServices()133 int ScanServices ()
134 {
135     int i;
136     int retVal = STATE_UNKNOWN;
137 
138     SnmpWalk ( baseOID, baseOIDLen );
139     for ( i = 0; i < scanResLen; i++ )
140     {
141         if ( scanRes [i].v0 )
142         {
143             retVal = STATE_OK;
144             printf ( "OK\n" );
145         }
146     }
147     if ( retVal == STATE_UNKNOWN ) printf ( "FAILED\n" );
148     return retVal;
149 }
150 
ReadVals()151 void ReadVals ()
152 {
153     struct snmp_session sess1;
154     struct snmp_session* sess2;
155     oid o_cond [12];
156     size_t oidLen;
157 
158     OidCpy ( o_cond, &oidLen, baseOID, &baseOIDLen );
159     o_cond [11] = 0;
160     oidLen = 12;
161     ConfigureSession1 ( &sess1 );
162     sess2 = snmp_open ( &sess1 );
163     if ( sess2 == NULL ) Error ( LOG_ERR, "Could not open session!" );
164     v.cond = ReadInteger ( sess2, o_cond, oidLen );
165     free ( sess1.peername );
166     free ( sess1.community );
167     snmp_close_sessions ();
168 }
169 
CreateDataFile(const char * aRRDFPath)170 void CreateDataFile ( const char* aRRDFPath )
171 {
172     /* performance data is not supported */
173 }
174 
ParseData(const char * aData)175 void ParseData ( const char* aData )
176 {
177     /* performance data is not supported */
178 }
179 
WriteData(const char * aRRDFPath)180 void WriteData ( const char* aRRDFPath )
181 {
182     /* performance data is not supported */
183 }
184 
CheckData()185 int CheckData ()
186 {
187     int retVal = STATE_UNKNOWN;
188 
189     ReadVals ();
190     switch ( v.cond )
191     {
192         case 1:
193             retVal = STATE_WARNING;
194             printf ( "WARNING - Thermal state is 'other'\n" );
195         break;
196         case 2:
197             retVal = STATE_OK;
198             printf ( "OK - Thermal state is 'ok'\n" );
199         break;
200         case 3:
201             retVal = STATE_WARNING;
202             printf ( "OK - Thermal state is 'degraded'\n" );
203         break;
204         case 4:
205             retVal = STATE_CRITICAL;
206             printf ( "OK - Thermal state is 'failed'\n" );
207         break;
208         default:
209             retVal = STATE_UNKNOWN;
210             printf ( "OK - Thermal state is invalid\n" );
211         break;
212     }
213     return ( retVal );
214 }
215 
main(int argc,char ** argv)216 int main ( int argc, char** argv )
217 {
218     int retVal = STATE_OK;
219 
220     SetGlobals ();
221     ReadArgs ( argc, argv );
222     if ( scanServices ) retVal = ScanServices ();
223     else
224     {
225         if ( checkData ) retVal = CheckData ();
226     }
227     if ( hostName != NULL ) free ( hostName );
228     if ( hostAddr != NULL ) free ( hostAddr );
229     if ( progName != NULL ) free ( progName );
230     return ( retVal );
231 }
232