check_ups.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 = "127.0.0.1";
  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 validate_arguments (void);
  96. void print_help (void);
  97. void print_usage (void);
  98. int
  99. main (int argc, char **argv)
  100. {
  101. int result = STATE_OK;
  102. char output_message[MAX_INPUT_BUFFER];
  103. char temp_buffer[MAX_INPUT_BUFFER];
  104. double ups_utility_deviation = 0.0L;
  105. if (process_arguments (argc, argv) != OK)
  106. usage ("Invalid command arguments supplied\n");
  107. /* initialize alarm signal handling */
  108. signal (SIGALRM, socket_timeout_alarm_handler);
  109. /* set socket timeout */
  110. alarm (socket_timeout);
  111. /* determine what variables the UPS supports */
  112. if (determine_supported_vars () != OK)
  113. return STATE_CRITICAL;
  114. /* get the ups status if possible */
  115. if (supported_options & UPS_STATUS) {
  116. if (determine_status () != OK)
  117. return STATE_CRITICAL;
  118. ups_status[0] = 0;
  119. result = STATE_OK;
  120. if (status & UPSSTATUS_OFF) {
  121. strcpy (ups_status, "Off");
  122. result = STATE_CRITICAL;
  123. }
  124. else if ((status & (UPSSTATUS_OB | UPSSTATUS_LB)) ==
  125. (UPSSTATUS_OB | UPSSTATUS_LB)) {
  126. strcpy (ups_status, "On Battery, Low Battery");
  127. result = STATE_CRITICAL;
  128. }
  129. else {
  130. if (status & UPSSTATUS_OL) {
  131. strcat (ups_status, "Online");
  132. }
  133. if (status & UPSSTATUS_OB) {
  134. strcat (ups_status, "On Battery");
  135. result = STATE_WARNING;
  136. }
  137. if (status & UPSSTATUS_LB) {
  138. strcat (ups_status, ", Low Battery");
  139. result = STATE_WARNING;
  140. }
  141. if (status & UPSSTATUS_CAL) {
  142. strcat (ups_status, ", Calibrating");
  143. }
  144. if (status & UPSSTATUS_UNKOWN) {
  145. strcat (ups_status, ", Unknown");
  146. }
  147. }
  148. }
  149. /* get the ups utility voltage if possible */
  150. if (supported_options & UPS_UTILITY) {
  151. if (get_ups_variable ("UTILITY", temp_buffer, sizeof (temp_buffer)) != OK)
  152. return STATE_CRITICAL;
  153. ups_utility_voltage = atof (temp_buffer);
  154. if (ups_utility_voltage > 120.0)
  155. ups_utility_deviation = 120.0 - ups_utility_voltage;
  156. else
  157. ups_utility_deviation = ups_utility_voltage - 120.0;
  158. if (check_variable == UPS_UTILITY) {
  159. if (check_critical_value == TRUE
  160. && ups_utility_deviation >= critical_value) result = STATE_CRITICAL;
  161. else if (check_warning_value == TRUE
  162. && ups_utility_deviation >= warning_value
  163. && result < STATE_WARNING) result = STATE_WARNING;
  164. }
  165. }
  166. /* get the ups battery percent if possible */
  167. if (supported_options & UPS_BATTPCT) {
  168. if (get_ups_variable ("BATTPCT", temp_buffer, sizeof (temp_buffer)) != OK)
  169. return STATE_CRITICAL;
  170. ups_battery_percent = atof (temp_buffer);
  171. if (check_variable == UPS_BATTPCT) {
  172. if (check_critical_value == TRUE
  173. && ups_battery_percent <= critical_value) result = STATE_CRITICAL;
  174. else if (check_warning_value == TRUE
  175. && ups_battery_percent <= warning_value
  176. && result < STATE_WARNING) result = STATE_WARNING;
  177. }
  178. }
  179. /* get the ups load percent if possible */
  180. if (supported_options & UPS_LOADPCT) {
  181. if (get_ups_variable ("LOADPCT", temp_buffer, sizeof (temp_buffer)) != OK)
  182. return STATE_CRITICAL;
  183. ups_load_percent = atof (temp_buffer);
  184. if (check_variable == UPS_LOADPCT) {
  185. if (check_critical_value == TRUE && ups_load_percent >= critical_value)
  186. result = STATE_CRITICAL;
  187. else if (check_warning_value == TRUE
  188. && ups_load_percent >= warning_value && result < STATE_WARNING)
  189. result = STATE_WARNING;
  190. }
  191. }
  192. /* get the ups temperature if possible */
  193. if (supported_options & UPS_TEMP) {
  194. if (get_ups_variable ("UPSTEMP", temp_buffer, sizeof (temp_buffer)) != OK)
  195. return STATE_CRITICAL;
  196. ups_temperature = (atof (temp_buffer) * 1.8) + 32;
  197. if (check_variable == UPS_TEMP) {
  198. if (check_critical_value == TRUE && ups_temperature >= critical_value)
  199. result = STATE_CRITICAL;
  200. else if (check_warning_value == TRUE && ups_temperature >= warning_value
  201. && result < STATE_WARNING)
  202. result = STATE_WARNING;
  203. }
  204. }
  205. /* if the UPS does not support any options we are looking for, report an error */
  206. if (supported_options == UPS_NONE)
  207. result = STATE_CRITICAL;
  208. /* reset timeout */
  209. alarm (0);
  210. sprintf (output_message, "UPS %s - ",
  211. (result == STATE_OK) ? "ok" : "problem");
  212. if (supported_options & UPS_STATUS) {
  213. sprintf (temp_buffer, "Status=%s ", ups_status);
  214. strcat (output_message, temp_buffer);
  215. }
  216. if (supported_options & UPS_UTILITY) {
  217. sprintf (temp_buffer, "Utility=%3.1fV ", ups_utility_voltage);
  218. strcat (output_message, temp_buffer);
  219. }
  220. if (supported_options & UPS_BATTPCT) {
  221. sprintf (temp_buffer, "Batt=%3.1f%% ", ups_battery_percent);
  222. strcat (output_message, temp_buffer);
  223. }
  224. if (supported_options & UPS_LOADPCT) {
  225. sprintf (temp_buffer, "Load=%3.1f%% ", ups_load_percent);
  226. strcat (output_message, temp_buffer);
  227. }
  228. if (supported_options & UPS_TEMP) {
  229. sprintf (temp_buffer, "Temp=%3.1fF", ups_temperature);
  230. strcat (output_message, temp_buffer);
  231. }
  232. if (supported_options == UPS_NONE) {
  233. sprintf (temp_buffer,
  234. "UPS does not appear to support any available options\n");
  235. strcat (output_message, temp_buffer);
  236. }
  237. printf ("%s\n", output_message);
  238. return result;
  239. }
  240. /* determines what options are supported by the UPS */
  241. int
  242. determine_status (void)
  243. {
  244. char recv_buffer[MAX_INPUT_BUFFER];
  245. char temp_buffer[MAX_INPUT_BUFFER];
  246. char *ptr;
  247. if (get_ups_variable ("STATUS", recv_buffer, sizeof (recv_buffer)) !=
  248. STATE_OK) {
  249. printf ("Invalid response received from hostn");
  250. return ERROR;
  251. }
  252. recv_buffer[strlen (recv_buffer) - 1] = 0;
  253. strcpy (temp_buffer, recv_buffer);
  254. for (ptr = (char *) strtok (temp_buffer, " "); ptr != NULL;
  255. ptr = (char *) strtok (NULL, " ")) {
  256. if (!strcmp (ptr, "OFF"))
  257. status |= UPSSTATUS_OFF;
  258. else if (!strcmp (ptr, "OL"))
  259. status |= UPSSTATUS_OL;
  260. else if (!strcmp (ptr, "OB"))
  261. status |= UPSSTATUS_OB;
  262. else if (!strcmp (ptr, "LB"))
  263. status |= UPSSTATUS_LB;
  264. else if (!strcmp (ptr, "CAL"))
  265. status |= UPSSTATUS_CAL;
  266. else
  267. status |= UPSSTATUS_UNKOWN;
  268. }
  269. return OK;
  270. }
  271. /* determines what options are supported by the UPS */
  272. int
  273. determine_supported_vars (void)
  274. {
  275. char send_buffer[MAX_INPUT_BUFFER];
  276. char recv_buffer[MAX_INPUT_BUFFER];
  277. char temp_buffer[MAX_INPUT_BUFFER];
  278. char *ptr;
  279. /* get the list of variables that this UPS supports */
  280. if (ups_name)
  281. sprintf (send_buffer, "LISTVARS %s\r\n", ups_name);
  282. else
  283. sprintf (send_buffer, "LISTVARS\r\n");
  284. if (process_tcp_request
  285. (server_address, server_port, send_buffer, recv_buffer,
  286. sizeof (recv_buffer)) != STATE_OK) {
  287. printf ("Invalid response received from host\n");
  288. return ERROR;
  289. }
  290. recv_buffer[strlen (recv_buffer) - 1] = 0;
  291. if (ups_name)
  292. ptr = recv_buffer + 5 + strlen (ups_name) + 2;
  293. else
  294. ptr = recv_buffer + 5;
  295. strcpy (temp_buffer, recv_buffer);
  296. for (ptr = (char *) strtok (temp_buffer, " "); ptr != NULL;
  297. ptr = (char *) strtok (NULL, " ")) {
  298. if (!strcmp (ptr, "UTILITY"))
  299. supported_options |= UPS_UTILITY;
  300. else if (!strcmp (ptr, "BATTPCT"))
  301. supported_options |= UPS_BATTPCT;
  302. else if (!strcmp (ptr, "LOADPCT"))
  303. supported_options |= UPS_LOADPCT;
  304. else if (!strcmp (ptr, "STATUS"))
  305. supported_options |= UPS_STATUS;
  306. else if (!strcmp (ptr, "UPSTEMP"))
  307. supported_options |= UPS_TEMP;
  308. }
  309. return OK;
  310. }
  311. /* gets a variable value for a specific UPS */
  312. int
  313. get_ups_variable (const char *varname, char *buf, int buflen)
  314. {
  315. /* char command[MAX_INPUT_BUFFER]; */
  316. char temp_buffer[MAX_INPUT_BUFFER];
  317. char send_buffer[MAX_INPUT_BUFFER];
  318. char *ptr;
  319. /* create the command string to send to the UPS daemon */
  320. if (ups_name)
  321. sprintf (send_buffer, "REQ %s@%s\n", varname, ups_name);
  322. else
  323. sprintf (send_buffer, "REQ %s\n", varname);
  324. /* send the command to the daemon and get a response back */
  325. if (process_tcp_request
  326. (server_address, server_port, send_buffer, temp_buffer,
  327. sizeof (temp_buffer)) != STATE_OK) {
  328. printf ("Invalid response received from host\n");
  329. return ERROR;
  330. }
  331. if (ups_name)
  332. ptr = temp_buffer + strlen (varname) + 5 + strlen (ups_name) + 1;
  333. else
  334. ptr = temp_buffer + strlen (varname) + 5;
  335. if (!strcmp (ptr, "NOT-SUPPORTED")) {
  336. printf ("Error: Variable '%s' is not supported\n", varname);
  337. return ERROR;
  338. }
  339. if (!strcmp (ptr, "DATA-STALE")) {
  340. printf ("Error: UPS data is stale\n");
  341. return ERROR;
  342. }
  343. if (!strcmp (ptr, "UNKNOWN-UPS")) {
  344. if (ups_name)
  345. printf ("Error: UPS '%s' is unknown\n", ups_name);
  346. else
  347. printf ("Error: UPS is unknown\n");
  348. return ERROR;
  349. }
  350. strncpy (buf, ptr, buflen - 1);
  351. buf[buflen - 1] = 0;
  352. return OK;
  353. }
  354. /* Command line: CHECK_UPS <host_address> [-u ups] [-p port] [-v variable]
  355. [-wv warn_value] [-cv crit_value] [-to to_sec] */
  356. /* process command-line arguments */
  357. int
  358. process_arguments (int argc, char **argv)
  359. {
  360. int c;
  361. #ifdef HAVE_GETOPT_H
  362. int option_index = 0;
  363. static struct option long_options[] = {
  364. {"hostname", required_argument, 0, 'H'},
  365. {"ups", required_argument, 0, 'u'},
  366. {"port", required_argument, 0, 'p'},
  367. {"critical", required_argument, 0, 'c'},
  368. {"warning", required_argument, 0, 'w'},
  369. {"timeout", required_argument, 0, 't'},
  370. {"variable", required_argument, 0, 'v'},
  371. {"version", no_argument, 0, 'V'},
  372. {"help", no_argument, 0, 'h'},
  373. {0, 0, 0, 0}
  374. };
  375. #endif
  376. if (argc < 2)
  377. return ERROR;
  378. for (c = 1; c < argc; c++) {
  379. if (strcmp ("-to", argv[c]) == 0)
  380. strcpy (argv[c], "-t");
  381. else if (strcmp ("-wt", argv[c]) == 0)
  382. strcpy (argv[c], "-w");
  383. else if (strcmp ("-ct", argv[c]) == 0)
  384. strcpy (argv[c], "-c");
  385. }
  386. while (1) {
  387. #ifdef HAVE_GETOPT_H
  388. c =
  389. getopt_long (argc, argv, "hVH:u:p:v:c:w:t:", long_options,
  390. &option_index);
  391. #else
  392. c = getopt (argc, argv, "hVH:u:p:v:c:w:t:");
  393. #endif
  394. if (c == -1 || c == EOF)
  395. break;
  396. switch (c) {
  397. case '?': /* help */
  398. usage3 ("Unknown option", optopt);
  399. case 'H': /* hostname */
  400. if (is_host (optarg)) {
  401. server_address = optarg;
  402. }
  403. else {
  404. usage2 ("Invalid host name", optarg);
  405. }
  406. break;
  407. case 'u': /* ups name */
  408. ups_name = optarg;
  409. break;
  410. case 'p': /* port */
  411. if (is_intpos (optarg)) {
  412. server_port = atoi (optarg);
  413. }
  414. else {
  415. usage2 ("Server port must be a positive integer", optarg);
  416. }
  417. break;
  418. case 'c': /* critical time threshold */
  419. if (is_intnonneg (optarg)) {
  420. critical_value = atoi (optarg);
  421. check_critical_value = TRUE;
  422. }
  423. else {
  424. usage2 ("Critical time must be a nonnegative integer", optarg);
  425. }
  426. break;
  427. case 'w': /* warning time threshold */
  428. if (is_intnonneg (optarg)) {
  429. warning_value = atoi (optarg);
  430. check_warning_value = TRUE;
  431. }
  432. else {
  433. usage2 ("Warning time must be a nonnegative integer", optarg);
  434. }
  435. break;
  436. case 'v': /* variable */
  437. if (!strcmp (optarg, "LINE"))
  438. check_variable = UPS_UTILITY;
  439. else if (!strcmp (optarg, "TEMP"))
  440. check_variable = UPS_TEMP;
  441. else if (!strcmp (optarg, "BATTPCT"))
  442. check_variable = UPS_BATTPCT;
  443. else if (!strcmp (optarg, "LOADPCT"))
  444. check_variable = UPS_LOADPCT;
  445. else
  446. usage2 ("Unrecognized UPS variable", optarg);
  447. break;
  448. case 't': /* timeout */
  449. if (is_intnonneg (optarg)) {
  450. socket_timeout = atoi (optarg);
  451. }
  452. else {
  453. usage ("Time interval must be a nonnegative integer\n");
  454. }
  455. break;
  456. case 'V': /* version */
  457. print_revision (PROGNAME, "$Revision$");
  458. exit (STATE_OK);
  459. case 'h': /* help */
  460. print_help ();
  461. exit (STATE_OK);
  462. }
  463. }
  464. if (server_address == NULL && argc > optind) {
  465. if (is_host (argv[optind]))
  466. server_address = argv[optind++];
  467. else
  468. usage ("Invalid host name");
  469. }
  470. return validate_arguments();
  471. }
  472. int
  473. validate_arguments (void)
  474. {
  475. return OK;
  476. }
  477. void
  478. print_help (void)
  479. {
  480. print_revision (PROGNAME, "$Revision$");
  481. printf
  482. ("Copyright (c) 2000 Tom Shields/Karl DeBisschop\n\n"
  483. "This plugin tests the UPS service on the specified host.\n"
  484. "Newtork UPS Tools for www.exploits.org must be running for this plugin to work.\n\n");
  485. print_usage ();
  486. printf
  487. ("\nOptions:\n"
  488. " -H, --hostname=STRING or IPADDRESS\n"
  489. " Check server on the indicated host\n"
  490. " -p, --port=INTEGER\n"
  491. " Make connection on the indicated port (default: %d)\n"
  492. " -u, --ups=STRING\n"
  493. " Name of UPS\n"
  494. " -w, --warning=INTEGER\n"
  495. " Seconds necessary to result in a warning status\n"
  496. " -c, --critical=INTEGER\n"
  497. " Seconds necessary to result in a critical status\n"
  498. " -t, --timeout=INTEGER\n"
  499. " Seconds before connection attempt times out (default: %d)\n"
  500. " -v, --verbose\n"
  501. " Print extra information (command-line use only)\n"
  502. " -h, --help\n"
  503. " Print detailed help screen\n"
  504. " -V, --version\n"
  505. " Print version information\n\n", PORT, DEFAULT_SOCKET_TIMEOUT);
  506. support ();
  507. }
  508. void
  509. print_usage (void)
  510. {
  511. printf
  512. ("Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit]\n"
  513. " [-t timeout] [-v]\n"
  514. " %s --help\n"
  515. " %s --version\n", PROGNAME, PROGNAME, PROGNAME);
  516. }