check_ups.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /******************************************************************************
  2. *
  3. * CHECK_UPS.C
  4. *
  5. * Program: UPS monitor plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  8. *
  9. * Last Modified: $Date$
  10. *
  11. * Command line: CHECK_UPS <host_address> [-u ups] [-p port] [-v variable] \
  12. * [-wv warn_value] [-cv crit_value] [-to to_sec]
  13. *
  14. * Description:
  15. *
  16. * This plugin attempts to determine the status of an UPS
  17. * (Uninterruptible Power Supply) on a remote host (or the local host)
  18. * that is being monitored with Russel Kroll's "Smarty UPS Tools"
  19. * package. If the UPS is online or calibrating, the plugin will
  20. * return an OK state. If the battery is on it will return a WARNING
  21. * state. If the UPS is off or has a low battery the plugin will
  22. * return a CRITICAL state. You may also specify a variable to check
  23. * (such as temperature, utility voltage, battery load, etc.) as well
  24. * as warning and critical thresholds for the value of that variable.
  25. * If the remote host has multiple UPS that are being monitored you
  26. * will have to use the [ups] option to specify which UPS to check.
  27. *
  28. * Notes:
  29. *
  30. * This plugin requires that the UPSD daemon distributed with Russel
  31. * Kroll's "Smart UPS Tools" be installed on the remote host. If you
  32. * don't have the package installed on your system, you can download
  33. * it from http://www.exploits.org/~rkroll/smartupstools
  34. *
  35. * License Information:
  36. *
  37. * This program is free software; you can redistribute it and/or modify
  38. * it under the terms of the GNU General Public License as published by
  39. * the Free Software Foundation; either version 2 of the License, or
  40. * (at your option) any later version.
  41. *
  42. * This program is distributed in the hope that it will be useful,
  43. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  44. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  45. * GNU General Public License for more details.
  46. *
  47. * You should have received a copy of the GNU General Public License
  48. * along with this program; if not, write to the Free Software
  49. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  50. *
  51. ******************************************************************************/
  52. #include "config.h"
  53. #include "common.h"
  54. #include "netutils.h"
  55. #include "utils.h"
  56. #define PROGNAME "check_ups"
  57. #define REVISION "$Revision$"
  58. #define COPYRIGHT "1999-2002"
  59. #define AUTHOR "Ethan Galstad"
  60. #define EMAIL "nagios@nagios.org"
  61. #define CHECK_NONE 0
  62. #define PORT 3305
  63. #define UPS_NONE 0 /* no supported options */
  64. #define UPS_UTILITY 1 /* supports utility line voltage */
  65. #define UPS_BATTPCT 2 /* supports percent battery remaining */
  66. #define UPS_STATUS 4 /* supports UPS status */
  67. #define UPS_TEMP 8 /* supports UPS temperature */
  68. #define UPS_LOADPCT 16 /* supports load percent */
  69. #define UPSSTATUS_NONE 0
  70. #define UPSSTATUS_OFF 1
  71. #define UPSSTATUS_OL 2
  72. #define UPSSTATUS_OB 4
  73. #define UPSSTATUS_LB 8
  74. #define UPSSTATUS_CAL 16
  75. #define UPSSTATUS_UNKOWN 32
  76. int server_port = PORT;
  77. char *server_address = NULL;
  78. char *ups_name = NULL;
  79. double warning_value = 0.0L;
  80. double critical_value = 0.0L;
  81. int check_warning_value = FALSE;
  82. int check_critical_value = FALSE;
  83. int check_variable = UPS_NONE;
  84. int supported_options = UPS_NONE;
  85. int status = UPSSTATUS_NONE;
  86. double ups_utility_voltage = 0.0L;
  87. double ups_battery_percent = 0.0L;
  88. double ups_load_percent = 0.0L;
  89. double ups_temperature = 0.0L;
  90. char ups_status[MAX_INPUT_BUFFER] = "N/A";
  91. int determine_status (void);
  92. int determine_supported_vars (void);
  93. int get_ups_variable (const char *, char *, int);
  94. int process_arguments (int, char **);
  95. int call_getopt (int, char **);
  96. int validate_arguments (void);
  97. void print_help (void);
  98. void print_usage (void);
  99. int
  100. main (int argc, char **argv)
  101. {
  102. int result = STATE_OK;
  103. char output_message[MAX_INPUT_BUFFER];
  104. char temp_buffer[MAX_INPUT_BUFFER];
  105. double ups_utility_deviation = 0.0L;
  106. if (process_arguments (argc, argv) != OK)
  107. usage ("Invalid command arguments supplied\n");
  108. /* initialize alarm signal handling */
  109. signal (SIGALRM, socket_timeout_alarm_handler);
  110. /* set socket timeout */
  111. alarm (socket_timeout);
  112. /* determine what variables the UPS supports */
  113. if (determine_supported_vars () != OK)
  114. return STATE_CRITICAL;
  115. /* get the ups status if possible */
  116. if (supported_options & UPS_STATUS) {
  117. if (determine_status () != OK)
  118. return STATE_CRITICAL;
  119. ups_status[0] = 0;
  120. result = STATE_OK;
  121. if (status & UPSSTATUS_OFF) {
  122. strcpy (ups_status, "Off");
  123. result = STATE_CRITICAL;
  124. }
  125. else if ((status & (UPSSTATUS_OB | UPSSTATUS_LB)) ==
  126. (UPSSTATUS_OB | UPSSTATUS_LB)) {
  127. strcpy (ups_status, "On Battery, Low Battery");
  128. result = STATE_CRITICAL;
  129. }
  130. else {
  131. if (status & UPSSTATUS_OL) {
  132. strcat (ups_status, "Online");
  133. }
  134. if (status & UPSSTATUS_OB) {
  135. strcat (ups_status, "On Battery");
  136. result = STATE_WARNING;
  137. }
  138. if (status & UPSSTATUS_LB) {
  139. strcat (ups_status, ", Low Battery");
  140. result = STATE_WARNING;
  141. }
  142. if (status & UPSSTATUS_CAL) {
  143. strcat (ups_status, ", Calibrating");
  144. }
  145. if (status & UPSSTATUS_UNKOWN) {
  146. strcat (ups_status, ", Unknown");
  147. }
  148. }
  149. }
  150. /* get the ups utility voltage if possible */
  151. if (supported_options & UPS_UTILITY) {
  152. if (get_ups_variable ("UTILITY", temp_buffer, sizeof (temp_buffer)) != OK)
  153. return STATE_CRITICAL;
  154. ups_utility_voltage = atof (temp_buffer);
  155. if (ups_utility_voltage > 120.0)
  156. ups_utility_deviation = 120.0 - ups_utility_voltage;
  157. else
  158. ups_utility_deviation = ups_utility_voltage - 120.0;
  159. if (check_variable == UPS_UTILITY) {
  160. if (check_critical_value == TRUE
  161. && ups_utility_deviation >= critical_value) result = STATE_CRITICAL;
  162. else if (check_warning_value == TRUE
  163. && ups_utility_deviation >= warning_value
  164. && result < STATE_WARNING) result = STATE_WARNING;
  165. }
  166. }
  167. /* get the ups battery percent if possible */
  168. if (supported_options & UPS_BATTPCT) {
  169. if (get_ups_variable ("BATTPCT", temp_buffer, sizeof (temp_buffer)) != OK)
  170. return STATE_CRITICAL;
  171. ups_battery_percent = atof (temp_buffer);
  172. if (check_variable == UPS_BATTPCT) {
  173. if (check_critical_value == TRUE
  174. && ups_battery_percent <= critical_value) result = STATE_CRITICAL;
  175. else if (check_warning_value == TRUE
  176. && ups_battery_percent <= warning_value
  177. && result < STATE_WARNING) result = STATE_WARNING;
  178. }
  179. }
  180. /* get the ups load percent if possible */
  181. if (supported_options & UPS_LOADPCT) {
  182. if (get_ups_variable ("LOADPCT", temp_buffer, sizeof (temp_buffer)) != OK)
  183. return STATE_CRITICAL;
  184. ups_load_percent = atof (temp_buffer);
  185. if (check_variable == UPS_LOADPCT) {
  186. if (check_critical_value == TRUE && ups_load_percent >= critical_value)
  187. result = STATE_CRITICAL;
  188. else if (check_warning_value == TRUE
  189. && ups_load_percent >= warning_value && result < STATE_WARNING)
  190. result = STATE_WARNING;
  191. }
  192. }
  193. /* get the ups temperature if possible */
  194. if (supported_options & UPS_TEMP) {
  195. if (get_ups_variable ("UPSTEMP", temp_buffer, sizeof (temp_buffer)) != OK)
  196. return STATE_CRITICAL;
  197. ups_temperature = (atof (temp_buffer) * 1.8) + 32;
  198. if (check_variable == UPS_TEMP) {
  199. if (check_critical_value == TRUE && ups_temperature >= critical_value)
  200. result = STATE_CRITICAL;
  201. else if (check_warning_value == TRUE && ups_temperature >= warning_value
  202. && result < STATE_WARNING)
  203. result = STATE_WARNING;
  204. }
  205. }
  206. /* if the UPS does not support any options we are looking for, report an error */
  207. if (supported_options == UPS_NONE)
  208. result = STATE_CRITICAL;
  209. /* reset timeout */
  210. alarm (0);
  211. sprintf (output_message, "UPS %s - ",
  212. (result == STATE_OK) ? "ok" : "problem");
  213. if (supported_options & UPS_STATUS) {
  214. sprintf (temp_buffer, "Status=%s ", ups_status);
  215. strcat (output_message, temp_buffer);
  216. }
  217. if (supported_options & UPS_UTILITY) {
  218. sprintf (temp_buffer, "Utility=%3.1fV ", ups_utility_voltage);
  219. strcat (output_message, temp_buffer);
  220. }
  221. if (supported_options & UPS_BATTPCT) {
  222. sprintf (temp_buffer, "Batt=%3.1f%% ", ups_battery_percent);
  223. strcat (output_message, temp_buffer);
  224. }
  225. if (supported_options & UPS_LOADPCT) {
  226. sprintf (temp_buffer, "Load=%3.1f%% ", ups_load_percent);
  227. strcat (output_message, temp_buffer);
  228. }
  229. if (supported_options & UPS_TEMP) {
  230. sprintf (temp_buffer, "Temp=%3.1fF", ups_temperature);
  231. strcat (output_message, temp_buffer);
  232. }
  233. if (supported_options == UPS_NONE) {
  234. sprintf (temp_buffer,
  235. "UPS does not appear to support any available options\n");
  236. strcat (output_message, temp_buffer);
  237. }
  238. printf ("%s\n", output_message);
  239. return result;
  240. }
  241. /* determines what options are supported by the UPS */
  242. int
  243. determine_status (void)
  244. {
  245. char recv_buffer[MAX_INPUT_BUFFER];
  246. char temp_buffer[MAX_INPUT_BUFFER];
  247. char *ptr;
  248. if (get_ups_variable ("STATUS", recv_buffer, sizeof (recv_buffer)) !=
  249. STATE_OK) {
  250. printf ("Invalid response received from hostn");
  251. return ERROR;
  252. }
  253. recv_buffer[strlen (recv_buffer) - 1] = 0;
  254. strcpy (temp_buffer, recv_buffer);
  255. for (ptr = (char *) strtok (temp_buffer, " "); ptr != NULL;
  256. ptr = (char *) strtok (NULL, " ")) {
  257. if (!strcmp (ptr, "OFF"))
  258. status |= UPSSTATUS_OFF;
  259. else if (!strcmp (ptr, "OL"))
  260. status |= UPSSTATUS_OL;
  261. else if (!strcmp (ptr, "OB"))
  262. status |= UPSSTATUS_OB;
  263. else if (!strcmp (ptr, "LB"))
  264. status |= UPSSTATUS_LB;
  265. else if (!strcmp (ptr, "CAL"))
  266. status |= UPSSTATUS_CAL;
  267. else
  268. status |= UPSSTATUS_UNKOWN;
  269. }
  270. return OK;
  271. }
  272. /* determines what options are supported by the UPS */
  273. int
  274. determine_supported_vars (void)
  275. {
  276. char send_buffer[MAX_INPUT_BUFFER];
  277. char recv_buffer[MAX_INPUT_BUFFER];
  278. char temp_buffer[MAX_INPUT_BUFFER];
  279. char *ptr;
  280. /* get the list of variables that this UPS supports */
  281. if (ups_name)
  282. sprintf (send_buffer, "LISTVARS %s\r\n", ups_name);
  283. else
  284. sprintf (send_buffer, "LISTVARS\r\n");
  285. if (process_tcp_request
  286. (server_address, server_port, send_buffer, recv_buffer,
  287. sizeof (recv_buffer)) != STATE_OK) {
  288. printf ("Invalid response received from host\n");
  289. return ERROR;
  290. }
  291. recv_buffer[strlen (recv_buffer) - 1] = 0;
  292. if (ups_name)
  293. ptr = recv_buffer + 5 + strlen (ups_name) + 2;
  294. else
  295. ptr = recv_buffer + 5;
  296. strcpy (temp_buffer, recv_buffer);
  297. for (ptr = (char *) strtok (temp_buffer, " "); ptr != NULL;
  298. ptr = (char *) strtok (NULL, " ")) {
  299. if (!strcmp (ptr, "UTILITY"))
  300. supported_options |= UPS_UTILITY;
  301. else if (!strcmp (ptr, "BATTPCT"))
  302. supported_options |= UPS_BATTPCT;
  303. else if (!strcmp (ptr, "LOADPCT"))
  304. supported_options |= UPS_LOADPCT;
  305. else if (!strcmp (ptr, "STATUS"))
  306. supported_options |= UPS_STATUS;
  307. else if (!strcmp (ptr, "UPSTEMP"))
  308. supported_options |= UPS_TEMP;
  309. }
  310. return OK;
  311. }
  312. /* gets a variable value for a specific UPS */
  313. int
  314. get_ups_variable (const char *varname, char *buf, int buflen)
  315. {
  316. /* char command[MAX_INPUT_BUFFER]; */
  317. char temp_buffer[MAX_INPUT_BUFFER];
  318. char send_buffer[MAX_INPUT_BUFFER];
  319. char *ptr;
  320. /* create the command string to send to the UPS daemon */
  321. if (ups_name)
  322. sprintf (send_buffer, "REQ %s@%s\n", varname, ups_name);
  323. else
  324. sprintf (send_buffer, "REQ %s\n", varname);
  325. /* send the command to the daemon and get a response back */
  326. if (process_tcp_request
  327. (server_address, server_port, send_buffer, temp_buffer,
  328. sizeof (temp_buffer)) != STATE_OK) {
  329. printf ("Invalid response received from host\n");
  330. return ERROR;
  331. }
  332. if (ups_name)
  333. ptr = temp_buffer + strlen (varname) + 5 + strlen (ups_name) + 1;
  334. else
  335. ptr = temp_buffer + strlen (varname) + 5;
  336. if (!strcmp (ptr, "NOT-SUPPORTED")) {
  337. printf ("Error: Variable '%s' is not supported\n", varname);
  338. return ERROR;
  339. }
  340. if (!strcmp (ptr, "DATA-STALE")) {
  341. printf ("Error: UPS data is stale\n");
  342. return ERROR;
  343. }
  344. if (!strcmp (ptr, "UNKNOWN-UPS")) {
  345. if (ups_name)
  346. printf ("Error: UPS '%s' is unknown\n", ups_name);
  347. else
  348. printf ("Error: UPS is unknown\n");
  349. return ERROR;
  350. }
  351. strncpy (buf, ptr, buflen - 1);
  352. buf[buflen - 1] = 0;
  353. return OK;
  354. }
  355. /* Command line: CHECK_UPS <host_address> [-u ups] [-p port] [-v variable]
  356. [-wv warn_value] [-cv crit_value] [-to to_sec] */
  357. /* process command-line arguments */
  358. int
  359. process_arguments (int argc, char **argv)
  360. {
  361. int c;
  362. #ifdef HAVE_GETOPT_H
  363. int option_index = 0;
  364. static struct option long_options[] = {
  365. {"hostname", required_argument, 0, 'H'},
  366. {"ups", required_argument, 0, 'u'},
  367. {"port", required_argument, 0, 'p'},
  368. {"critical", required_argument, 0, 'c'},
  369. {"warning", required_argument, 0, 'w'},
  370. {"timeout", required_argument, 0, 't'},
  371. {"variable", required_argument, 0, 'v'},
  372. {"version", no_argument, 0, 'V'},
  373. {"help", no_argument, 0, 'h'},
  374. {0, 0, 0, 0}
  375. };
  376. #endif
  377. if (argc < 2)
  378. return ERROR;
  379. for (c = 1; c < argc; c++) {
  380. if (strcmp ("-to", argv[c]) == 0)
  381. strcpy (argv[c], "-t");
  382. else if (strcmp ("-wt", argv[c]) == 0)
  383. strcpy (argv[c], "-w");
  384. else if (strcmp ("-ct", argv[c]) == 0)
  385. strcpy (argv[c], "-c");
  386. }
  387. while (1) {
  388. #ifdef HAVE_GETOPT_H
  389. c =
  390. getopt_long (argc, argv, "hVH:u:p:v:c:w:t:", long_options,
  391. &option_index);
  392. #else
  393. c = getopt (argc, argv, "hVH:u:p:v:c:w:t:");
  394. #endif
  395. if (c == -1 || c == EOF)
  396. break;
  397. switch (c) {
  398. case '?': /* help */
  399. usage3 ("Unknown option", optopt);
  400. case 'H': /* hostname */
  401. if (is_host (optarg)) {
  402. server_address = optarg;
  403. }
  404. else {
  405. usage2 ("Invalid host name", optarg);
  406. }
  407. break;
  408. case 'u': /* ups name */
  409. ups_name = optarg;
  410. break;
  411. case 'p': /* port */
  412. if (is_intpos (optarg)) {
  413. server_port = atoi (optarg);
  414. }
  415. else {
  416. usage2 ("Server port must be a positive integer", optarg);
  417. }
  418. break;
  419. case 'c': /* critical time threshold */
  420. if (is_intnonneg (optarg)) {
  421. critical_value = atoi (optarg);
  422. check_critical_value = TRUE;
  423. }
  424. else {
  425. usage2 ("Critical time must be a nonnegative integer", optarg);
  426. }
  427. break;
  428. case 'w': /* warning time threshold */
  429. if (is_intnonneg (optarg)) {
  430. warning_value = atoi (optarg);
  431. check_warning_value = TRUE;
  432. }
  433. else {
  434. usage2 ("Warning time must be a nonnegative integer", optarg);
  435. }
  436. break;
  437. case 'v': /* variable */
  438. if (!strcmp (optarg, "LINE"))
  439. check_variable = UPS_UTILITY;
  440. else if (!strcmp (optarg, "TEMP"))
  441. check_variable = UPS_TEMP;
  442. else if (!strcmp (optarg, "BATTPCT"))
  443. check_variable = UPS_BATTPCT;
  444. else if (!strcmp (optarg, "LOADPCT"))
  445. check_variable = UPS_LOADPCT;
  446. else
  447. usage2 ("Unrecognized UPS variable", optarg);
  448. break;
  449. case 't': /* timeout */
  450. if (is_intnonneg (optarg)) {
  451. socket_timeout = atoi (optarg);
  452. }
  453. else {
  454. usage ("Time interval must be a nonnegative integer\n");
  455. }
  456. break;
  457. case 'V': /* version */
  458. print_revision (PROGNAME, "$Revision$");
  459. exit (STATE_OK);
  460. case 'h': /* help */
  461. print_help ();
  462. exit (STATE_OK);
  463. }
  464. }
  465. if (server_address == NULL) {
  466. if (optind >= argc) {
  467. server_address = strscpy (NULL, "127.0.0.1");
  468. }
  469. else if (is_host (argv[optind])) {
  470. server_address = argv[optind++];
  471. }
  472. else {
  473. usage ("Invalid host name");
  474. }
  475. }
  476. return validate_arguments();
  477. }
  478. int
  479. validate_arguments (void)
  480. {
  481. return OK;
  482. }
  483. void
  484. print_help (void)
  485. {
  486. print_revision (PROGNAME, "$Revision$");
  487. printf
  488. ("Copyright (c) 2000 Tom Shields/Karl DeBisschop\n\n"
  489. "This plugin tests the UPS service on the specified host.\n"
  490. "Newtork UPS Tools for www.exploits.org must be running for this plugin to work.\n\n");
  491. print_usage ();
  492. printf
  493. ("\nOptions:\n"
  494. " -H, --hostname=STRING or IPADDRESS\n"
  495. " Check server on the indicated host\n"
  496. " -p, --port=INTEGER\n"
  497. " Make connection on the indicated port (default: %d)\n"
  498. " -u, --ups=STRING\n"
  499. " Name of UPS\n"
  500. " -w, --warning=INTEGER\n"
  501. " Seconds necessary to result in a warning status\n"
  502. " -c, --critical=INTEGER\n"
  503. " Seconds necessary to result in a critical status\n"
  504. " -t, --timeout=INTEGER\n"
  505. " Seconds before connection attempt times out (default: %d)\n"
  506. " -v, --verbose\n"
  507. " Print extra information (command-line use only)\n"
  508. " -h, --help\n"
  509. " Print detailed help screen\n"
  510. " -V, --version\n"
  511. " Print version information\n\n", PORT, DEFAULT_SOCKET_TIMEOUT);
  512. support ();
  513. }
  514. void
  515. print_usage (void)
  516. {
  517. printf
  518. ("Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit]\n"
  519. " [-t timeout] [-v]\n"
  520. " %s --help\n"
  521. " %s --version\n", PROGNAME, PROGNAME, PROGNAME);
  522. }