check_hpjd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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", PATH_TO_SNMPGET, community,
  110. address, 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. temp_buffer = strtok (input_buffer, "=");
  129. temp_buffer = strtok (NULL, "=");
  130. if (temp_buffer == NULL && line < 13) {
  131. result = STATE_UNKNOWN;
  132. strcpy (errmsg, input_buffer);
  133. } else {
  134. switch (line) {
  135. case 1: /* 1st line should contain the line status */
  136. line_status = atoi (temp_buffer);
  137. break;
  138. case 2: /* 2nd line should contain the paper status */
  139. paper_status = atoi (temp_buffer);
  140. break;
  141. case 3: /* 3rd line should be intervention required */
  142. intervention_required = atoi (temp_buffer);
  143. break;
  144. case 4: /* 4th line should be peripheral error */
  145. peripheral_error = atoi (temp_buffer);
  146. break;
  147. case 5: /* 5th line should contain the paper jam status */
  148. paper_jam = atoi (temp_buffer);
  149. break;
  150. case 6: /* 6th line should contain the paper out status */
  151. paper_out = atoi (temp_buffer);
  152. break;
  153. case 7: /* 7th line should contain the toner low status */
  154. toner_low = atoi (temp_buffer);
  155. break;
  156. case 8: /* did data come too slow for engine */
  157. page_punt = atoi (temp_buffer);
  158. break;
  159. case 9: /* did we run out of memory */
  160. memory_out = atoi (temp_buffer);
  161. break;
  162. case 10: /* is there a door open */
  163. door_open = atoi (temp_buffer);
  164. break;
  165. case 11: /* is output tray full */
  166. paper_output = atoi (temp_buffer);
  167. break;
  168. case 12: /* display panel message */
  169. strcpy (display_message, temp_buffer + 1);
  170. if (
  171. (flawcorrection) &&
  172. (!strcmp(display_message,"READY")) &&
  173. (!paper_status) &&
  174. (!intervention_required) &&
  175. (!peripheral_error) &&
  176. (!paper_jam) &&
  177. (!paper_out)
  178. )
  179. line_status = 0;
  180. break;
  181. default: /* fold multiline message */
  182. strncat (display_message, input_buffer,
  183. sizeof (display_message) - strlen (display_message) - 1);
  184. }
  185. }
  186. /* break out of the read loop if we encounter an error */
  187. if (result != STATE_OK)
  188. break;
  189. }
  190. /* WARNING if output found on stderr */
  191. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  192. result = max_state (result, STATE_WARNING);
  193. /* remove CRLF */
  194. if (input_buffer[strlen (input_buffer) - 1] == '\n')
  195. input_buffer[strlen (input_buffer) - 1] = 0;
  196. sprintf (errmsg, "%s", input_buffer );
  197. }
  198. /* close stderr */
  199. (void) fclose (child_stderr);
  200. /* close the pipe */
  201. if (spclose (child_process))
  202. result = max_state (result, STATE_WARNING);
  203. /* if there wasn't any output, display an error */
  204. if (line == 0) {
  205. /* might not be the problem, but most likely is. */
  206. result = STATE_UNKNOWN ;
  207. xasprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address );
  208. }
  209. /* if we had no read errors, check the printer status results... */
  210. if (result == STATE_OK) {
  211. if (paper_jam) {
  212. result = STATE_WARNING;
  213. strcpy (errmsg, _("Paper Jam"));
  214. }
  215. else if (paper_out) {
  216. result = STATE_WARNING;
  217. strcpy (errmsg, _("Out of Paper"));
  218. }
  219. else if (line_status == OFFLINE) {
  220. if (strcmp (errmsg, "POWERSAVE ON") != 0) {
  221. result = STATE_WARNING;
  222. strcpy (errmsg, _("Printer Offline"));
  223. }
  224. }
  225. else if (peripheral_error) {
  226. result = STATE_WARNING;
  227. strcpy (errmsg, _("Peripheral Error"));
  228. }
  229. else if (intervention_required) {
  230. result = STATE_WARNING;
  231. strcpy (errmsg, _("Intervention Required"));
  232. }
  233. else if (toner_low) {
  234. result = STATE_WARNING;
  235. strcpy (errmsg, _("Toner Low"));
  236. }
  237. else if (memory_out) {
  238. result = STATE_WARNING;
  239. strcpy (errmsg, _("Insufficient Memory"));
  240. }
  241. else if (door_open) {
  242. result = STATE_WARNING;
  243. strcpy (errmsg, _("A Door is Open"));
  244. }
  245. else if (paper_output) {
  246. result = STATE_WARNING;
  247. strcpy (errmsg, _("Output Tray is Full"));
  248. }
  249. else if (page_punt) {
  250. result = STATE_WARNING;
  251. strcpy (errmsg, _("Data too Slow for Engine"));
  252. }
  253. else if (paper_status) {
  254. result = STATE_WARNING;
  255. strcpy (errmsg, _("Unknown Paper Error"));
  256. }
  257. }
  258. if (result == STATE_OK)
  259. printf (_("Printer ok - (%s)\n"), display_message);
  260. else if (result == STATE_UNKNOWN) {
  261. printf ("%s\n", errmsg);
  262. /* if printer could not be reached, escalate to critical */
  263. if (strstr (errmsg, "Timeout"))
  264. result = STATE_CRITICAL;
  265. }
  266. else if (result == STATE_WARNING)
  267. printf ("%s (%s)\n", errmsg, display_message);
  268. return result;
  269. }
  270. /* process command-line arguments */
  271. int
  272. process_arguments (int argc, char **argv)
  273. {
  274. int c;
  275. int option = 0;
  276. static struct option longopts[] = {
  277. {"hostname", required_argument, 0, 'H'},
  278. {"community", required_argument, 0, 'C'},
  279. /* {"critical", required_argument,0,'c'}, */
  280. /* {"warning", required_argument,0,'w'}, */
  281. {"port", required_argument,0,'p'},
  282. {"flawcorrection", no_argument, 0, 'N'},
  283. {"version", no_argument, 0, 'V'},
  284. {"help", no_argument, 0, 'h'},
  285. {0, 0, 0, 0}
  286. };
  287. if (argc < 2)
  288. return ERROR;
  289. while (1) {
  290. c = getopt_long (argc, argv, "+hVNH:C:p:", longopts, &option);
  291. if (c == -1 || c == EOF || c == 1)
  292. break;
  293. switch (c) {
  294. case 'H': /* hostname */
  295. if (is_host (optarg)) {
  296. address = strscpy(address, optarg) ;
  297. }
  298. else {
  299. usage2 (_("Invalid hostname/address"), optarg);
  300. }
  301. break;
  302. case 'C': /* community */
  303. community = strscpy (community, optarg);
  304. break;
  305. case 'p':
  306. if (!is_intpos(optarg))
  307. usage2 (_("Port must be a positive short integer"), optarg);
  308. else
  309. port = atoi(optarg);
  310. break;
  311. case 'N': /* flaw correction */
  312. flawcorrection=1;
  313. break;
  314. case 'V': /* version */
  315. print_revision (progname, NP_VERSION);
  316. exit (STATE_OK);
  317. case 'h': /* help */
  318. print_help ();
  319. exit (STATE_OK);
  320. case '?': /* help */
  321. usage5 ();
  322. }
  323. }
  324. c = optind;
  325. if (address == NULL) {
  326. if (is_host (argv[c])) {
  327. address = argv[c++];
  328. }
  329. else {
  330. usage2 (_("Invalid hostname/address"), argv[c]);
  331. }
  332. }
  333. if (community == NULL) {
  334. if (argv[c] != NULL )
  335. community = argv[c++];
  336. else
  337. community = strdup (DEFAULT_COMMUNITY);
  338. }
  339. if (port == 0) {
  340. if (argv[c] != NULL )
  341. port = atoi (argv[c++]);
  342. else
  343. port = atoi (DEFAULT_PORT);
  344. }
  345. return validate_arguments ();
  346. }
  347. int
  348. validate_arguments (void)
  349. {
  350. return OK;
  351. }
  352. void
  353. print_help (void)
  354. {
  355. print_revision (progname, NP_VERSION);
  356. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  357. printf (COPYRIGHT, copyright, email);
  358. printf ("%s\n", _("This plugin tests the STATUS of an HP printer with a JetDirect card."));
  359. printf ("%s\n", _("Net-snmp must be installed on the computer running the plugin."));
  360. printf ("\n\n");
  361. print_usage ();
  362. printf (UT_HELP_VRSN);
  363. printf (UT_EXTRA_OPTS);
  364. printf (" %s\n", "-C, --community=STRING");
  365. printf (" %s", _("The SNMP community name "));
  366. printf (_("(default=%s)"), DEFAULT_COMMUNITY);
  367. printf ("\n");
  368. printf (" %s\n", "-p, --port=STRING");
  369. printf (" %s", _("Specify the port to check "));
  370. printf (_("(default=%s)"), DEFAULT_PORT);
  371. printf ("\n");
  372. printf (" %s\n", "-N, --flawcorrection");
  373. printf (" %s", _("Correct false offline status reports "));
  374. printf (_("(default=%s)"), "false");
  375. printf ("\n");
  376. printf (UT_SUPPORT);
  377. }
  378. void
  379. print_usage (void)
  380. {
  381. printf ("%s\n", _("Usage:"));
  382. printf ("%s -H host [-C community] [-p port][-N]\n", progname);
  383. }