check_hpjd.c 10 KB

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