4
0

check_hpjd.c 11 KB

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