check_ups.c 16 KB

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