check_hpjd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*****************************************************************************
  2. *
  3. * Nagios check_hpjd plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2014 Nagios Plugins Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains the check_hpjd plugin
  11. *
  12. * This plugin tests the STATUS of an HP printer with a JetDirect card.
  13. * Net-SNMP must be installed on the computer running the plugin.
  14. *
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. *
  30. *****************************************************************************/
  31. const char *progname = "check_hpjd";
  32. const char *copyright = "2000-2014";
  33. const char *email = "devel@nagios-plugins.org";
  34. #include "common.h"
  35. #include "popen.h"
  36. #include "utils.h"
  37. #include "netutils.h"
  38. #define DEFAULT_COMMUNITY "public"
  39. #define DEFAULT_PORT "161"
  40. const char *option_summary = "-H host [-C community]\n";
  41. #define HPJD_LINE_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
  42. #define HPJD_PAPER_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
  43. #define HPJD_INTERVENTION_REQUIRED ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
  44. #define HPJD_GD_PERIPHERAL_ERROR ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
  45. #define HPJD_GD_PAPER_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
  46. #define HPJD_GD_PAPER_JAM ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
  47. #define HPJD_GD_TONER_LOW ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
  48. #define HPJD_GD_PAGE_PUNT ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
  49. #define HPJD_GD_MEMORY_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
  50. #define HPJD_GD_DOOR_OPEN ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
  51. #define HPJD_GD_PAPER_OUTPUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
  52. #define HPJD_GD_STATUS_DISPLAY ".1.3.6.1.4.1.11.2.3.9.1.1.3"
  53. #define ONLINE 0
  54. #define OFFLINE 1
  55. int process_arguments (int, char **);
  56. int validate_arguments (void);
  57. void print_help (void);
  58. void print_usage (void);
  59. char *community = NULL;
  60. char *address = NULL;
  61. int port = 0;
  62. char flawcorrection =0; // There are devices which report offline when that is not the case. Correct that.
  63. int
  64. main (int argc, char **argv)
  65. {
  66. char command_line[1024];
  67. int result = STATE_UNKNOWN;
  68. int line;
  69. char input_buffer[MAX_INPUT_BUFFER];
  70. char query_string[512];
  71. char *errmsg;
  72. char *temp_buffer;
  73. int line_status = ONLINE;
  74. int paper_status = 0;
  75. int intervention_required = 0;
  76. int peripheral_error = 0;
  77. int paper_jam = 0;
  78. int paper_out = 0;
  79. int toner_low = 0;
  80. int page_punt = 0;
  81. int memory_out = 0;
  82. int door_open = 0;
  83. int paper_output = 0;
  84. char display_message[MAX_INPUT_BUFFER];
  85. errmsg = malloc(MAX_INPUT_BUFFER);
  86. setlocale (LC_ALL, "");
  87. bindtextdomain (PACKAGE, LOCALEDIR);
  88. textdomain (PACKAGE);
  89. /* Parse extra opts if any */
  90. argv=np_extra_opts (&argc, argv, progname);
  91. if (process_arguments (argc, argv) == ERROR)
  92. usage4 (_("Could not parse arguments"));
  93. /* removed ' 2>1' at end of command 10/27/1999 - EG */
  94. /* create the query string */
  95. sprintf
  96. (query_string,
  97. "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0",
  98. HPJD_LINE_STATUS,
  99. HPJD_PAPER_STATUS,
  100. HPJD_INTERVENTION_REQUIRED,
  101. HPJD_GD_PERIPHERAL_ERROR,
  102. HPJD_GD_PAPER_JAM,
  103. HPJD_GD_PAPER_OUT,
  104. HPJD_GD_TONER_LOW,
  105. HPJD_GD_PAGE_PUNT,
  106. HPJD_GD_MEMORY_OUT,
  107. HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);
  108. /* get the command to run */
  109. sprintf (command_line, "%s -OQa -m : -v 1 -c %s %s:%hd %s",
  110. PATH_TO_SNMPGET, community, address, (short)port, query_string);
  111. /* run the command */
  112. child_process = spopen (command_line);
  113. if (child_process == NULL) {
  114. printf (_("Could not open pipe: %s\n"), command_line);
  115. return STATE_UNKNOWN;
  116. }
  117. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  118. if (child_stderr == NULL) {
  119. printf (_("Could not open stderr for %s\n"), command_line);
  120. }
  121. result = STATE_OK;
  122. line = 0;
  123. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  124. /* strip the newline character from the end of the input */
  125. if (input_buffer[strlen (input_buffer) - 1] == '\n')
  126. input_buffer[strlen (input_buffer) - 1] = 0;
  127. line++;
  128. /* We want the string after the "=" */
  129. (void)strtok (input_buffer, "=");
  130. temp_buffer = strtok (NULL, "=");
  131. if (temp_buffer == NULL && line < 13) {
  132. result = STATE_UNKNOWN;
  133. strcpy (errmsg, input_buffer);
  134. } else {
  135. switch (line) {
  136. case 1: /* 1st line should contain the line status */
  137. line_status = atoi (temp_buffer);
  138. break;
  139. case 2: /* 2nd line should contain the paper status */
  140. paper_status = atoi (temp_buffer);
  141. break;
  142. case 3: /* 3rd line should be intervention required */
  143. intervention_required = atoi (temp_buffer);
  144. break;
  145. case 4: /* 4th line should be peripheral error */
  146. peripheral_error = atoi (temp_buffer);
  147. break;
  148. case 5: /* 5th line should contain the paper jam status */
  149. paper_jam = atoi (temp_buffer);
  150. break;
  151. case 6: /* 6th line should contain the paper out status */
  152. paper_out = atoi (temp_buffer);
  153. break;
  154. case 7: /* 7th line should contain the toner low status */
  155. toner_low = atoi (temp_buffer);
  156. break;
  157. case 8: /* did data come too slow for engine */
  158. page_punt = atoi (temp_buffer);
  159. break;
  160. case 9: /* did we run out of memory */
  161. memory_out = atoi (temp_buffer);
  162. break;
  163. case 10: /* is there a door open */
  164. door_open = atoi (temp_buffer);
  165. break;
  166. case 11: /* is output tray full */
  167. paper_output = atoi (temp_buffer);
  168. break;
  169. case 12: /* display panel message */
  170. strcpy (display_message, temp_buffer + 1);
  171. if (
  172. (flawcorrection) &&
  173. (!strcmp(display_message,"READY")) &&
  174. (!paper_status) &&
  175. (!intervention_required) &&
  176. (!peripheral_error) &&
  177. (!paper_jam) &&
  178. (!paper_out)
  179. )
  180. line_status = 0;
  181. break;
  182. default: /* fold multiline message */
  183. strncat (display_message, input_buffer,
  184. sizeof (display_message) - strlen (display_message) - 1);
  185. }
  186. }
  187. /* break out of the read loop if we encounter an error */
  188. if (result != STATE_OK)
  189. break;
  190. }
  191. /* WARNING if output found on stderr */
  192. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  193. result = max_state (result, STATE_WARNING);
  194. /* remove CRLF */
  195. if (input_buffer[strlen (input_buffer) - 1] == '\n')
  196. input_buffer[strlen (input_buffer) - 1] = 0;
  197. sprintf (errmsg, "%s", input_buffer );
  198. }
  199. /* close stderr */
  200. (void) fclose (child_stderr);
  201. /* close the pipe */
  202. if (spclose (child_process))
  203. result = max_state (result, STATE_WARNING);
  204. /* if there wasn't any output, display an error */
  205. if (line == 0) {
  206. /* might not be the problem, but most likely is. */
  207. result = STATE_UNKNOWN ;
  208. xasprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address );
  209. }
  210. /* if we had no read errors, check the printer status results... */
  211. if (result == STATE_OK) {
  212. if (paper_jam) {
  213. result = STATE_WARNING;
  214. strcpy (errmsg, _("Paper Jam"));
  215. }
  216. else if (paper_out) {
  217. result = STATE_WARNING;
  218. strcpy (errmsg, _("Out of Paper"));
  219. }
  220. else if (line_status == OFFLINE) {
  221. if (strcmp (errmsg, "POWERSAVE ON") != 0) {
  222. result = STATE_WARNING;
  223. strcpy (errmsg, _("Printer Offline"));
  224. }
  225. }
  226. else if (peripheral_error) {
  227. result = STATE_WARNING;
  228. strcpy (errmsg, _("Peripheral Error"));
  229. }
  230. else if (intervention_required) {
  231. result = STATE_WARNING;
  232. strcpy (errmsg, _("Intervention Required"));
  233. }
  234. else if (toner_low) {
  235. result = STATE_WARNING;
  236. strcpy (errmsg, _("Toner Low"));
  237. }
  238. else if (memory_out) {
  239. result = STATE_WARNING;
  240. strcpy (errmsg, _("Insufficient Memory"));
  241. }
  242. else if (door_open) {
  243. result = STATE_WARNING;
  244. strcpy (errmsg, _("A Door is Open"));
  245. }
  246. else if (paper_output) {
  247. result = STATE_WARNING;
  248. strcpy (errmsg, _("Output Tray is Full"));
  249. }
  250. else if (page_punt) {
  251. result = STATE_WARNING;
  252. strcpy (errmsg, _("Data too Slow for Engine"));
  253. }
  254. else if (paper_status) {
  255. result = STATE_WARNING;
  256. strcpy (errmsg, _("Unknown Paper Error"));
  257. }
  258. }
  259. if (result == STATE_OK)
  260. printf (_("Printer ok - (%s)\n"), display_message);
  261. else if (result == STATE_UNKNOWN) {
  262. printf ("%s\n", errmsg);
  263. /* if printer could not be reached, escalate to critical */
  264. if (strstr (errmsg, "Timeout"))
  265. result = STATE_CRITICAL;
  266. }
  267. else if (result == STATE_WARNING)
  268. printf ("%s (%s)\n", errmsg, display_message);
  269. return result;
  270. }
  271. /* process command-line arguments */
  272. int
  273. process_arguments (int argc, char **argv)
  274. {
  275. int c;
  276. int option = 0;
  277. static struct option longopts[] = {
  278. {"hostname", required_argument, 0, 'H'},
  279. {"community", required_argument, 0, 'C'},
  280. /* {"critical", required_argument,0,'c'}, */
  281. /* {"warning", required_argument,0,'w'}, */
  282. {"port", required_argument,0,'p'},
  283. {"flawcorrection", no_argument, 0, 'N'},
  284. {"version", no_argument, 0, 'V'},
  285. {"help", no_argument, 0, 'h'},
  286. {0, 0, 0, 0}
  287. };
  288. if (argc < 2)
  289. return ERROR;
  290. while (1) {
  291. c = getopt_long (argc, argv, "+hVNH:C:p:", longopts, &option);
  292. if (c == -1 || c == EOF || c == 1)
  293. break;
  294. switch (c) {
  295. case 'H': /* hostname */
  296. if (is_host (optarg)) {
  297. address = strscpy(address, optarg) ;
  298. }
  299. else {
  300. usage2 (_("Invalid hostname/address"), optarg);
  301. }
  302. break;
  303. case 'C': /* community */
  304. community = strscpy (community, optarg);
  305. break;
  306. case 'p':
  307. if (!is_intpos(optarg))
  308. usage2 (_("Port must be a positive short integer"), optarg);
  309. else
  310. port = atoi(optarg);
  311. break;
  312. case 'N': /* flaw correction */
  313. flawcorrection=1;
  314. break;
  315. case 'V': /* version */
  316. print_revision (progname, NP_VERSION);
  317. exit (STATE_OK);
  318. case 'h': /* help */
  319. print_help ();
  320. exit (STATE_OK);
  321. case '?': /* help */
  322. usage5 ();
  323. }
  324. }
  325. c = optind;
  326. if (address == NULL) {
  327. if (is_host (argv[c])) {
  328. address = argv[c++];
  329. }
  330. else {
  331. usage2 (_("Invalid hostname/address"), argv[c]);
  332. }
  333. }
  334. if (community == NULL) {
  335. if (argv[c] != NULL )
  336. community = argv[c++];
  337. else
  338. community = strdup (DEFAULT_COMMUNITY);
  339. }
  340. if (port == 0) {
  341. if (argv[c] != NULL )
  342. port = atoi (argv[c++]);
  343. else
  344. port = atoi (DEFAULT_PORT);
  345. }
  346. return validate_arguments ();
  347. }
  348. int
  349. validate_arguments (void)
  350. {
  351. return OK;
  352. }
  353. void
  354. print_help (void)
  355. {
  356. print_revision (progname, NP_VERSION);
  357. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  358. printf (COPYRIGHT, copyright, email);
  359. printf ("%s\n", _("This plugin tests the STATUS of an HP printer with a JetDirect card."));
  360. printf ("%s\n", _("Net-snmp must be installed on the computer running the plugin."));
  361. printf ("\n\n");
  362. print_usage ();
  363. printf (UT_HELP_VRSN);
  364. printf (UT_EXTRA_OPTS);
  365. printf (" %s\n", "-C, --community=STRING");
  366. printf (" %s", _("The SNMP community name "));
  367. printf (_("(default=%s)"), DEFAULT_COMMUNITY);
  368. printf ("\n");
  369. printf (" %s\n", "-p, --port=STRING");
  370. printf (" %s", _("Specify the port to check "));
  371. printf (_("(default=%s)"), DEFAULT_PORT);
  372. printf ("\n");
  373. printf (" %s\n", "-N, --flawcorrection");
  374. printf (" %s", _("Correct false offline status reports "));
  375. printf (_("(default=%s)"), "false");
  376. printf ("\n");
  377. printf (UT_SUPPORT);
  378. }
  379. void
  380. print_usage (void)
  381. {
  382. printf ("%s\n", _("Usage:"));
  383. printf ("%s -H host [-C community] [-p port][-N]\n", progname);
  384. }