4
0

check_ups.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*****************************************************************************
  2. *
  3. * Nagios check_ups plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000 Tom Shields
  7. * 2004 Alain Richard <alain.richard@equation.fr>
  8. * 2004 Arnaud Quette <arnaud.quette@mgeups.com>
  9. * Copyright (c) 2002-2014 Nagios Plugins Development Team
  10. *
  11. * Description:
  12. *
  13. * This file contains Network UPS Tools plugin for Nagios
  14. *
  15. * This plugin tests the UPS service on the specified host. Network UPS Tools
  16. * from www.networkupstools.org must be running for this plugin to work.
  17. *
  18. *
  19. * This program is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation, either version 3 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. *
  32. *
  33. *****************************************************************************/
  34. const char *progname = "check_ups";
  35. const char *copyright = "2000-2014";
  36. const char *email = "devel@nagios-plugins.org";
  37. #include "common.h"
  38. #include "netutils.h"
  39. #include "utils.h"
  40. enum {
  41. PORT = 3493
  42. };
  43. #define CHECK_NONE 0
  44. #define UPS_NONE 0 /* no supported options */
  45. #define UPS_UTILITY 1 /* supports utility line voltage */
  46. #define UPS_BATTPCT 2 /* supports percent battery remaining */
  47. #define UPS_STATUS 4 /* supports UPS status */
  48. #define UPS_TEMP 8 /* supports UPS temperature */
  49. #define UPS_LOADPCT 16 /* supports load percent */
  50. #define UPS_BATTLEFT 32 /* supports runtime left */
  51. #define UPSSTATUS_NONE 0
  52. #define UPSSTATUS_OFF 1
  53. #define UPSSTATUS_OL 2
  54. #define UPSSTATUS_OB 4
  55. #define UPSSTATUS_LB 8
  56. #define UPSSTATUS_CAL 16
  57. #define UPSSTATUS_RB 32 /*Replace Battery */
  58. #define UPSSTATUS_BYPASS 64
  59. #define UPSSTATUS_OVER 128
  60. #define UPSSTATUS_TRIM 256
  61. #define UPSSTATUS_BOOST 512
  62. #define UPSSTATUS_CHRG 1024
  63. #define UPSSTATUS_DISCHRG 2048
  64. #define UPSSTATUS_UNKNOWN 4096
  65. enum { NOSUCHVAR = ERROR-1 };
  66. int server_port = PORT;
  67. char *server_address;
  68. char *ups_name = NULL;
  69. double warning_value = 0.0;
  70. double critical_value = 0.0;
  71. int check_warn = FALSE;
  72. int check_crit = FALSE;
  73. int check_variable = UPS_NONE;
  74. int supported_options = UPS_NONE;
  75. int status = UPSSTATUS_NONE;
  76. static int extended_units = 0;
  77. double ups_utility_voltage = 0.0;
  78. double ups_battery_percent = 0.0;
  79. double ups_load_percent = 0.0;
  80. double ups_temperature = 0.0;
  81. double ups_battery_left = 0.0;
  82. char *ups_status;
  83. int temp_output_c = 0;
  84. int determine_status (void);
  85. int get_ups_variable (const char *, char *, size_t);
  86. int process_arguments (int, char **);
  87. int validate_arguments (void);
  88. void print_help (void);
  89. void print_usage (void);
  90. int
  91. main (int argc, char **argv)
  92. {
  93. int result = STATE_UNKNOWN;
  94. char *message;
  95. char *data;
  96. char *tunits;
  97. char temp_buffer[MAX_INPUT_BUFFER];
  98. double ups_utility_deviation = 0.0;
  99. int res;
  100. setlocale (LC_ALL, "");
  101. bindtextdomain (PACKAGE, LOCALEDIR);
  102. textdomain (PACKAGE);
  103. ups_status = strdup ("N/A");
  104. data = strdup ("");
  105. message = strdup ("");
  106. /* Parse extra opts if any */
  107. argv=np_extra_opts (&argc, argv, progname);
  108. if (process_arguments (argc, argv) == ERROR)
  109. usage4 (_("Could not parse arguments"));
  110. /* initialize alarm signal handling */
  111. signal (SIGALRM, socket_timeout_alarm_handler);
  112. /* set socket timeout */
  113. alarm (timeout_interval);
  114. /* get the ups status if possible */
  115. if (determine_status () != OK)
  116. return STATE_CRITICAL;
  117. if (supported_options & UPS_STATUS) {
  118. ups_status = strdup ("");
  119. result = STATE_OK;
  120. if (status & UPSSTATUS_OFF) {
  121. xasprintf (&ups_status, "Off");
  122. result = STATE_CRITICAL;
  123. }
  124. else if ((status & (UPSSTATUS_OB | UPSSTATUS_LB)) ==
  125. (UPSSTATUS_OB | UPSSTATUS_LB)) {
  126. xasprintf (&ups_status, _("On Battery, Low Battery"));
  127. result = STATE_CRITICAL;
  128. }
  129. else {
  130. if (status & UPSSTATUS_OL) {
  131. xasprintf (&ups_status, "%s%s", ups_status, _("Online"));
  132. }
  133. if (status & UPSSTATUS_OB) {
  134. xasprintf (&ups_status, "%s%s", ups_status, _("On Battery"));
  135. result = STATE_WARNING;
  136. }
  137. if (status & UPSSTATUS_LB) {
  138. xasprintf (&ups_status, "%s%s", ups_status, _(", Low Battery"));
  139. result = STATE_WARNING;
  140. }
  141. if (status & UPSSTATUS_CAL) {
  142. xasprintf (&ups_status, "%s%s", ups_status, _(", Calibrating"));
  143. }
  144. if (status & UPSSTATUS_RB) {
  145. xasprintf (&ups_status, "%s%s", ups_status, _(", Replace Battery"));
  146. result = STATE_WARNING;
  147. }
  148. if (status & UPSSTATUS_BYPASS) {
  149. xasprintf (&ups_status, "%s%s", ups_status, _(", On Bypass"));
  150. }
  151. if (status & UPSSTATUS_OVER) {
  152. xasprintf (&ups_status, "%s%s", ups_status, _(", Overload"));
  153. }
  154. if (status & UPSSTATUS_TRIM) {
  155. xasprintf (&ups_status, "%s%s", ups_status, _(", Trimming"));
  156. }
  157. if (status & UPSSTATUS_BOOST) {
  158. xasprintf (&ups_status, "%s%s", ups_status, _(", Boosting"));
  159. }
  160. if (status & UPSSTATUS_CHRG) {
  161. xasprintf (&ups_status, "%s%s", ups_status, _(", Charging"));
  162. }
  163. if (status & UPSSTATUS_DISCHRG) {
  164. xasprintf (&ups_status, "%s%s", ups_status, _(", Discharging"));
  165. }
  166. if (status & UPSSTATUS_UNKNOWN) {
  167. xasprintf (&ups_status, "%s%s", ups_status, _(", Unknown"));
  168. }
  169. }
  170. xasprintf (&message, "%sStatus=%s ", message, ups_status);
  171. }
  172. /* get the ups utility voltage if possible */
  173. res=get_ups_variable ("input.voltage", temp_buffer, sizeof (temp_buffer));
  174. if (res == NOSUCHVAR) supported_options &= ~UPS_UTILITY;
  175. else if (res != OK)
  176. return STATE_CRITICAL;
  177. else {
  178. supported_options |= UPS_UTILITY;
  179. ups_utility_voltage = atof (temp_buffer);
  180. xasprintf (&message, "%sUtility=%3.1fV ", message, ups_utility_voltage);
  181. if (ups_utility_voltage > 120.0)
  182. ups_utility_deviation = 120.0 - ups_utility_voltage;
  183. else
  184. ups_utility_deviation = ups_utility_voltage - 120.0;
  185. if (check_variable == UPS_UTILITY) {
  186. if (check_crit==TRUE && ups_utility_deviation>=critical_value) {
  187. result = STATE_CRITICAL;
  188. }
  189. else if (check_warn==TRUE && ups_utility_deviation>=warning_value) {
  190. result = max_state (result, STATE_WARNING);
  191. }
  192. xasprintf (&data, "%s",
  193. fperfdata ("voltage", ups_utility_voltage, (extended_units ? "V" : ""),
  194. check_warn, warning_value,
  195. check_crit, critical_value,
  196. TRUE, 0, FALSE, 0));
  197. } else {
  198. xasprintf (&data, "%s",
  199. fperfdata ("voltage", ups_utility_voltage, (extended_units ? "V" : ""),
  200. FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
  201. }
  202. }
  203. /* get the ups battery percent if possible */
  204. res=get_ups_variable ("battery.charge", temp_buffer, sizeof (temp_buffer));
  205. if (res == NOSUCHVAR) supported_options &= ~UPS_BATTPCT;
  206. else if ( res != OK)
  207. return STATE_CRITICAL;
  208. else {
  209. supported_options |= UPS_BATTPCT;
  210. ups_battery_percent = atof (temp_buffer);
  211. xasprintf (&message, "%sBatt=%3.1f%% ", message, ups_battery_percent);
  212. if (check_variable == UPS_BATTPCT) {
  213. if (check_crit==TRUE && ups_battery_percent <= critical_value) {
  214. result = STATE_CRITICAL;
  215. }
  216. else if (check_warn==TRUE && ups_battery_percent<=warning_value) {
  217. result = max_state (result, STATE_WARNING);
  218. }
  219. xasprintf (&data, "%s %s", data,
  220. fperfdata ("battery", ups_battery_percent, "%",
  221. check_warn, warning_value,
  222. check_crit, critical_value,
  223. TRUE, 0, TRUE, 100));
  224. } else {
  225. xasprintf (&data, "%s %s", data,
  226. fperfdata ("battery", ups_battery_percent, "%",
  227. FALSE, 0, FALSE, 0, TRUE, 0, TRUE, 100));
  228. }
  229. }
  230. /* get the ups load percent if possible */
  231. res=get_ups_variable ("ups.load", temp_buffer, sizeof (temp_buffer));
  232. if ( res == NOSUCHVAR ) supported_options &= ~UPS_LOADPCT;
  233. else if ( res != OK)
  234. return STATE_CRITICAL;
  235. else {
  236. supported_options |= UPS_LOADPCT;
  237. ups_load_percent = atof (temp_buffer);
  238. xasprintf (&message, "%sLoad=%3.1f%% ", message, ups_load_percent);
  239. if (check_variable == UPS_LOADPCT) {
  240. if (check_crit==TRUE && ups_load_percent>=critical_value) {
  241. result = STATE_CRITICAL;
  242. }
  243. else if (check_warn==TRUE && ups_load_percent>=warning_value) {
  244. result = max_state (result, STATE_WARNING);
  245. }
  246. xasprintf (&data, "%s %s", data,
  247. fperfdata ("load", ups_load_percent, "%",
  248. check_warn, warning_value,
  249. check_crit, critical_value,
  250. TRUE, 0, TRUE, 100));
  251. } else {
  252. xasprintf (&data, "%s %s", data,
  253. fperfdata ("load", ups_load_percent, "%",
  254. FALSE, 0, FALSE, 0, TRUE, 0, TRUE, 100));
  255. }
  256. }
  257. /* get the ups temperature if possible */
  258. res=get_ups_variable ("ups.temperature", temp_buffer, sizeof (temp_buffer));
  259. if ( res == NOSUCHVAR ) supported_options &= ~UPS_TEMP;
  260. else if ( res != OK)
  261. return STATE_CRITICAL;
  262. else {
  263. supported_options |= UPS_TEMP;
  264. if (temp_output_c) {
  265. tunits="degC";
  266. ups_temperature = atof (temp_buffer);
  267. xasprintf (&message, "%sTemp=%3.1fC ", message, ups_temperature);
  268. }
  269. else {
  270. tunits="degF";
  271. ups_temperature = (atof (temp_buffer) * 1.8) + 32;
  272. xasprintf (&message, "%sTemp=%3.1fF ", message, ups_temperature);
  273. }
  274. if (check_variable == UPS_TEMP) {
  275. if (check_crit==TRUE && ups_temperature>=critical_value) {
  276. result = STATE_CRITICAL;
  277. }
  278. else if (check_warn == TRUE && ups_temperature>=warning_value) {
  279. result = max_state (result, STATE_WARNING);
  280. }
  281. xasprintf (&data, "%s %s", data,
  282. fperfdata ("temp", ups_temperature, (extended_units ? tunits : ""),
  283. check_warn, warning_value,
  284. check_crit, critical_value,
  285. TRUE, 0, FALSE, 0));
  286. } else {
  287. xasprintf (&data, "%s %s", data,
  288. fperfdata ("temp", ups_temperature, (extended_units ? tunits : ""),
  289. FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
  290. }
  291. }
  292. /* get the ups battery runtime left if possible */
  293. res=get_ups_variable ("battery.runtime", temp_buffer, sizeof (temp_buffer));
  294. if (res == NOSUCHVAR) supported_options &= ~UPS_BATTLEFT;
  295. else if ( res != OK)
  296. return STATE_CRITICAL;
  297. else {
  298. supported_options |= UPS_BATTLEFT;
  299. ups_battery_left = atof (temp_buffer) / 60;
  300. xasprintf (&message, "%sLeft=%3.1fmin", message, ups_battery_left);
  301. if (check_variable == UPS_BATTLEFT) {
  302. if (check_crit==TRUE && ups_battery_left <= critical_value) {
  303. result = STATE_CRITICAL;
  304. }
  305. else if (check_warn==TRUE && ups_battery_left<=warning_value) {
  306. result = max_state (result, STATE_WARNING);
  307. }
  308. xasprintf (&data, "%s %s", data,
  309. fperfdata ("left", ups_battery_left, "",
  310. check_warn, warning_value,
  311. check_crit, critical_value,
  312. TRUE, 0, FALSE, 0));
  313. } else {
  314. xasprintf (&data, "%s %s", data,
  315. fperfdata ("left", ups_battery_left, "",
  316. FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
  317. }
  318. }
  319. /* if the UPS does not support any options we are looking for, report an error */
  320. if (supported_options == UPS_NONE) {
  321. result = STATE_CRITICAL;
  322. xasprintf (&message, _("UPS does not support any available options\n"));
  323. }
  324. /* reset timeout */
  325. alarm (0);
  326. printf ("UPS %s - %s|%s\n", state_text(result), message, data);
  327. return result;
  328. }
  329. /* determines what options are supported by the UPS */
  330. int
  331. determine_status (void)
  332. {
  333. char recv_buffer[MAX_INPUT_BUFFER];
  334. char temp_buffer[MAX_INPUT_BUFFER];
  335. char *ptr;
  336. int res;
  337. res=get_ups_variable ("ups.status", recv_buffer, sizeof (recv_buffer));
  338. if (res == NOSUCHVAR) return OK;
  339. if (res != STATE_OK) {
  340. printf ("%s\n", _("Invalid response received from host"));
  341. return ERROR;
  342. }
  343. supported_options |= UPS_STATUS;
  344. strcpy (temp_buffer, recv_buffer);
  345. for (ptr = (char *) strtok (temp_buffer, " "); ptr != NULL;
  346. ptr = (char *) strtok (NULL, " ")) {
  347. if (!strcmp (ptr, "OFF"))
  348. status |= UPSSTATUS_OFF;
  349. else if (!strcmp (ptr, "OL"))
  350. status |= UPSSTATUS_OL;
  351. else if (!strcmp (ptr, "OB"))
  352. status |= UPSSTATUS_OB;
  353. else if (!strcmp (ptr, "LB"))
  354. status |= UPSSTATUS_LB;
  355. else if (!strcmp (ptr, "CAL"))
  356. status |= UPSSTATUS_CAL;
  357. else if (!strcmp (ptr, "RB"))
  358. status |= UPSSTATUS_RB;
  359. else if (!strcmp (ptr, "BYPASS"))
  360. status |= UPSSTATUS_BYPASS;
  361. else if (!strcmp (ptr, "OVER"))
  362. status |= UPSSTATUS_OVER;
  363. else if (!strcmp (ptr, "TRIM"))
  364. status |= UPSSTATUS_TRIM;
  365. else if (!strcmp (ptr, "BOOST"))
  366. status |= UPSSTATUS_BOOST;
  367. else if (!strcmp (ptr, "CHRG"))
  368. status |= UPSSTATUS_CHRG;
  369. else if (!strcmp (ptr, "DISCHRG"))
  370. status |= UPSSTATUS_DISCHRG;
  371. else
  372. status |= UPSSTATUS_UNKNOWN;
  373. }
  374. return OK;
  375. }
  376. /* gets a variable value for a specific UPS */
  377. int
  378. get_ups_variable (const char *varname, char *buf, size_t buflen)
  379. {
  380. /* char command[MAX_INPUT_BUFFER]; */
  381. char temp_buffer[MAX_INPUT_BUFFER];
  382. char send_buffer[MAX_INPUT_BUFFER];
  383. char *ptr;
  384. char *logout = "OK Goodbye\n";
  385. int logout_len = strlen(logout);
  386. int len;
  387. *buf=0;
  388. /* create the command string to send to the UPS daemon */
  389. /* Add LOGOUT to avoid read failure logs */
  390. sprintf (send_buffer, "GET VAR %s %s\nLOGOUT\n", ups_name, varname);
  391. /* send the command to the daemon and get a response back */
  392. if (process_tcp_request
  393. (server_address, server_port, send_buffer, temp_buffer,
  394. sizeof (temp_buffer)) != STATE_OK) {
  395. printf ("%s\n", _("Invalid response received from host"));
  396. return ERROR;
  397. }
  398. ptr = temp_buffer;
  399. len = strlen(ptr);
  400. if (len > logout_len && strcmp (ptr + len - logout_len, logout) == 0) len -= logout_len;
  401. if (len > 0 && ptr[len-1] == '\n') ptr[len-1]=0;
  402. if (strcmp (ptr, "ERR UNKNOWN-UPS") == 0) {
  403. printf (_("CRITICAL - no such UPS '%s' on that host\n"), ups_name);
  404. return ERROR;
  405. }
  406. if (strcmp (ptr, "ERR VAR-NOT-SUPPORTED") == 0) {
  407. /*printf ("Error: Variable '%s' is not supported\n", varname);*/
  408. return NOSUCHVAR;
  409. }
  410. if (strcmp (ptr, "ERR DATA-STALE") == 0) {
  411. printf ("%s\n", _("CRITICAL - UPS data is stale"));
  412. return ERROR;
  413. }
  414. if (strncmp (ptr, "ERR", 3) == 0) {
  415. printf (_("Unknown error: %s\n"), ptr);
  416. return ERROR;
  417. }
  418. ptr = temp_buffer + strlen (varname) + strlen (ups_name) + 6;
  419. len = strlen(ptr);
  420. if (len < 2 || ptr[0] != '"' || ptr[len-1] != '"') {
  421. printf ("%s\n", _("Error: unable to parse variable"));
  422. return ERROR;
  423. }
  424. strncpy (buf, ptr+1, len - 2);
  425. buf[len - 2] = 0;
  426. return OK;
  427. }
  428. /* Command line: CHECK_UPS -H <host_address> -u ups [-p port] [-v variable]
  429. [-wv warn_value] [-cv crit_value] [-to to_sec] */
  430. /* process command-line arguments */
  431. int
  432. process_arguments (int argc, char **argv)
  433. {
  434. int c;
  435. int option = 0;
  436. static struct option longopts[] = {
  437. {"hostname", required_argument, 0, 'H'},
  438. {"ups", required_argument, 0, 'u'},
  439. {"port", required_argument, 0, 'p'},
  440. {"critical", required_argument, 0, 'c'},
  441. {"warning", required_argument, 0, 'w'},
  442. {"extended-units", no_argument, 0, 'e'},
  443. {"timeout", required_argument, 0, 't'},
  444. {"temperature", no_argument, 0, 'T'},
  445. {"variable", required_argument, 0, 'v'},
  446. {"version", no_argument, 0, 'V'},
  447. {"help", no_argument, 0, 'h'},
  448. {0, 0, 0, 0}
  449. };
  450. if (argc < 2)
  451. return ERROR;
  452. for (c = 1; c < argc; c++) {
  453. if (strcmp ("-to", argv[c]) == 0)
  454. strcpy (argv[c], "-t");
  455. else if (strcmp ("-wt", argv[c]) == 0)
  456. strcpy (argv[c], "-w");
  457. else if (strcmp ("-ct", argv[c]) == 0)
  458. strcpy (argv[c], "-c");
  459. }
  460. while (1) {
  461. c = getopt_long (argc, argv, "hVTH:u:p:v:c:w:t:", longopts,
  462. &option);
  463. if (c == -1 || c == EOF)
  464. break;
  465. switch (c) {
  466. case '?': /* help */
  467. usage5 ();
  468. case 'H': /* hostname */
  469. if (is_host (optarg)) {
  470. server_address = optarg;
  471. }
  472. else {
  473. usage2 (_("Invalid hostname/address"), optarg);
  474. }
  475. break;
  476. case 'T': /* FIXME: to be improved (ie "-T C" for Celsius or "-T F" for Farenheit) */
  477. temp_output_c = 1;
  478. break;
  479. case 'u': /* ups name */
  480. ups_name = optarg;
  481. break;
  482. case 'p': /* port */
  483. if (is_intpos (optarg)) {
  484. server_port = atoi (optarg);
  485. }
  486. else {
  487. usage2 (_("Port must be a positive integer"), optarg);
  488. }
  489. break;
  490. case 'c': /* critical time threshold */
  491. if (is_intnonneg (optarg)) {
  492. critical_value = atoi (optarg);
  493. check_crit = TRUE;
  494. }
  495. else {
  496. usage2 (_("Critical time must be a positive integer"), optarg);
  497. }
  498. break;
  499. case 'w': /* warning time threshold */
  500. if (is_intnonneg (optarg)) {
  501. warning_value = atoi (optarg);
  502. check_warn = TRUE;
  503. }
  504. else {
  505. usage2 (_("Warning time must be a positive integer"), optarg);
  506. }
  507. break;
  508. case 'e':
  509. extended_units = 1;
  510. break;
  511. case 'v': /* variable */
  512. if (!strcmp (optarg, "LINE"))
  513. check_variable = UPS_UTILITY;
  514. else if (!strcmp (optarg, "TEMP"))
  515. check_variable = UPS_TEMP;
  516. else if (!strcmp (optarg, "BATTLEFT"))
  517. check_variable = UPS_BATTLEFT;
  518. else if (!strcmp (optarg, "BATTPCT"))
  519. check_variable = UPS_BATTPCT;
  520. else if (!strcmp (optarg, "LOADPCT"))
  521. check_variable = UPS_LOADPCT;
  522. else
  523. usage2 (_("Unrecognized UPS variable"), optarg);
  524. break;
  525. case 't': /* timeout */
  526. timeout_interval = parse_timeout_string (optarg);
  527. break;
  528. case 'V': /* version */
  529. print_revision (progname, NP_VERSION);
  530. exit (STATE_OK);
  531. case 'h': /* help */
  532. print_help ();
  533. exit (STATE_OK);
  534. }
  535. }
  536. if (server_address == NULL && argc > optind) {
  537. if (is_host (argv[optind]))
  538. server_address = argv[optind++];
  539. else
  540. usage2 (_("Invalid hostname/address"), optarg);
  541. }
  542. if (server_address == NULL)
  543. server_address = strdup("127.0.0.1");
  544. return validate_arguments();
  545. }
  546. int
  547. validate_arguments (void)
  548. {
  549. if (! ups_name) {
  550. printf ("%s\n", _("Error : no UPS indicated"));
  551. return ERROR;
  552. }
  553. return OK;
  554. }
  555. void
  556. print_help (void)
  557. {
  558. char *myport;
  559. xasprintf (&myport, "%d", PORT);
  560. print_revision (progname, NP_VERSION);
  561. printf ("Copyright (c) 2000 Tom Shields\n");
  562. printf ("Copyright (c) 2004 Alain Richard <alain.richard@equation.fr>\n");
  563. printf ("Copyright (c) 2004 Arnaud Quette <arnaud.quette@mgeups.com>\n");
  564. printf (COPYRIGHT, copyright, email);
  565. printf ("%s\n", _("This plugin tests the UPS service on the specified host. Network UPS Tools"));
  566. printf ("%s\n", _("from www.networkupstools.org must be running for this plugin to work."));
  567. printf ("\n\n");
  568. print_usage ();
  569. printf (UT_HELP_VRSN);
  570. printf (UT_EXTRA_OPTS);
  571. printf (UT_HOST_PORT, 'p', myport);
  572. printf (" %s\n", "-u, --ups=STRING");
  573. printf (" %s\n", _("Name of UPS"));
  574. printf (" %s\n", "-T, --temperature");
  575. printf (" %s\n", _("Output of temperatures in Celsius"));
  576. printf (" %s\n", "-e, --extended-units");
  577. printf (" %s\n", _("Allow nonstandard units in performance data (used for voltage and temperatures)"));
  578. printf (" %s\n", "-v, --variable=STRING");
  579. printf (" %s %s\n", _("Valid values for STRING are"), "LINE, TEMP, BATTLEFT, BATTPCT or LOADPCT");
  580. printf (UT_WARN_CRIT);
  581. printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
  582. /* TODO: -v clashing with -v/-variable. Commenting out help text since verbose
  583. is unused up to now */
  584. /* printf (UT_VERBOSE); */
  585. printf ("\n");
  586. printf ("%s\n", _("This plugin attempts to determine the status of a UPS (Uninterruptible Power"));
  587. printf ("%s\n", _("Supply) on a local or remote host. If the UPS is online or calibrating, the"));
  588. printf ("%s\n", _("plugin will return an OK state. If the battery is on it will return a WARNING"));
  589. printf ("%s\n", _("state. If the UPS is off or has a low battery the plugin will return a CRITICAL"));
  590. printf ("%s\n", _("state."));
  591. printf ("\n");
  592. printf ("%s\n", _("Notes:"));
  593. printf (" %s\n", _("You may also specify a variable to check (such as temperature, utility voltage,"));
  594. printf (" %s\n", _("battery load, etc.) as well as warning and critical thresholds for the value"));
  595. printf (" %s\n", _("of that variable. If the remote host has multiple UPS that are being monitored"));
  596. printf (" %s\n", _("you will have to use the --ups option to specify which UPS to check."));
  597. printf ("\n");
  598. printf (" %s\n", _("This plugin requires that the UPSD daemon distributed with Russell Kroll's"));
  599. printf (" %s\n", _("Network UPS Tools be installed on the remote host. If you do not have the"));
  600. printf (" %s\n", _("package installed on your system, you can download it from"));
  601. printf (" %s\n", _("http://www.networkupstools.org"));
  602. printf (UT_SUPPORT);
  603. }
  604. void
  605. print_usage (void)
  606. {
  607. printf ("%s\n", _("Usage:"));
  608. printf ("%s -H host -u ups [-p port] [-v variable] [-w warn_value] [-c crit_value] [-e] [-to to_sec] [-T]\n", progname);
  609. }