check_mrtg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*****************************************************************************
  2. *
  3. * Nagios check_mrtg plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2007 Nagios Plugins Development Team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_mrtg plugin
  13. *
  14. * This plugin will check either the average or maximum value of one of the
  15. * two variables recorded in an MRTG log file.
  16. *
  17. *
  18. * This program is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation, either version 3 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. *
  31. * $Id$
  32. *
  33. *****************************************************************************/
  34. const char *progname = "check_mrtg";
  35. const char *revision = "$Revision$";
  36. const char *copyright = "1999-2007";
  37. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  38. #include "common.h"
  39. #include "utils.h"
  40. int process_arguments (int, char **);
  41. int validate_arguments (void);
  42. void print_help (void);
  43. void print_usage (void);
  44. char *log_file = NULL;
  45. int expire_minutes = 0;
  46. int use_average = TRUE;
  47. int variable_number = -1;
  48. unsigned long value_warning_threshold = 0L;
  49. unsigned long value_critical_threshold = 0L;
  50. char *label;
  51. char *units;
  52. int
  53. main (int argc, char **argv)
  54. {
  55. int result = STATE_UNKNOWN;
  56. FILE *fp;
  57. int line;
  58. char input_buffer[MAX_INPUT_BUFFER];
  59. char *temp_buffer;
  60. time_t current_time;
  61. time_t timestamp = 0L;
  62. unsigned long average_value_rate = 0L;
  63. unsigned long maximum_value_rate = 0L;
  64. unsigned long rate = 0L;
  65. setlocale (LC_ALL, "");
  66. bindtextdomain (PACKAGE, LOCALEDIR);
  67. textdomain (PACKAGE);
  68. if (process_arguments (argc, argv) == ERROR)
  69. usage4 (_("Could not parse arguments\n"));
  70. /* open the MRTG log file for reading */
  71. fp = fopen (log_file, "r");
  72. if (fp == NULL) {
  73. printf (_("Unable to open MRTG log file\n"));
  74. return STATE_UNKNOWN;
  75. }
  76. line = 0;
  77. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  78. line++;
  79. /* skip the first line of the log file */
  80. if (line == 1)
  81. continue;
  82. /* break out of read loop if we've passed the number of entries we want to read */
  83. if (line > 2)
  84. break;
  85. /* grab the timestamp */
  86. temp_buffer = strtok (input_buffer, " ");
  87. timestamp = strtoul (temp_buffer, NULL, 10);
  88. /* grab the average value 1 rate */
  89. temp_buffer = strtok (NULL, " ");
  90. if (variable_number == 1)
  91. average_value_rate = strtoul (temp_buffer, NULL, 10);
  92. /* grab the average value 2 rate */
  93. temp_buffer = strtok (NULL, " ");
  94. if (variable_number == 2)
  95. average_value_rate = strtoul (temp_buffer, NULL, 10);
  96. /* grab the maximum value 1 rate */
  97. temp_buffer = strtok (NULL, " ");
  98. if (variable_number == 1)
  99. maximum_value_rate = strtoul (temp_buffer, NULL, 10);
  100. /* grab the maximum value 2 rate */
  101. temp_buffer = strtok (NULL, " ");
  102. if (variable_number == 2)
  103. maximum_value_rate = strtoul (temp_buffer, NULL, 10);
  104. }
  105. /* close the log file */
  106. fclose (fp);
  107. /* if we couldn't read enough data, return an unknown error */
  108. if (line <= 2) {
  109. printf (_("Unable to process MRTG log file\n"));
  110. return STATE_UNKNOWN;
  111. }
  112. /* make sure the MRTG data isn't too old */
  113. time (&current_time);
  114. if (expire_minutes > 0
  115. && (current_time - timestamp) > (expire_minutes * 60)) {
  116. printf (_("MRTG data has expired (%d minutes old)\n"),
  117. (int) ((current_time - timestamp) / 60));
  118. return STATE_WARNING;
  119. }
  120. /* else check the incoming/outgoing rates */
  121. if (use_average == TRUE)
  122. rate = average_value_rate;
  123. else
  124. rate = maximum_value_rate;
  125. if (rate > value_critical_threshold)
  126. result = STATE_CRITICAL;
  127. else if (rate > value_warning_threshold)
  128. result = STATE_WARNING;
  129. printf("%s. %s = %lu %s|%s\n",
  130. (use_average == TRUE) ? _("Avg") : _("Max"),
  131. label, rate, units,
  132. perfdata(label, (long) rate, units,
  133. (int) value_warning_threshold, (long) value_warning_threshold,
  134. (int) value_critical_threshold, (long) value_critical_threshold,
  135. 0, 0, 0, 0));
  136. return result;
  137. }
  138. /* process command-line arguments */
  139. int
  140. process_arguments (int argc, char **argv)
  141. {
  142. int c;
  143. int option = 0;
  144. static struct option longopts[] = {
  145. {"logfile", required_argument, 0, 'F'},
  146. {"expires", required_argument, 0, 'e'},
  147. {"aggregation", required_argument, 0, 'a'},
  148. {"variable", required_argument, 0, 'v'},
  149. {"critical", required_argument, 0, 'c'},
  150. {"warning", required_argument, 0, 'w'},
  151. {"label", required_argument, 0, 'l'},
  152. {"units", required_argument, 0, 'u'},
  153. {"verbose", no_argument, 0, 'v'},
  154. {"version", no_argument, 0, 'V'},
  155. {"help", no_argument, 0, 'h'},
  156. {0, 0, 0, 0}
  157. };
  158. if (argc < 2)
  159. return ERROR;
  160. for (c = 1; c < argc; c++) {
  161. if (strcmp ("-to", argv[c]) == 0)
  162. strcpy (argv[c], "-t");
  163. else if (strcmp ("-wt", argv[c]) == 0)
  164. strcpy (argv[c], "-w");
  165. else if (strcmp ("-ct", argv[c]) == 0)
  166. strcpy (argv[c], "-c");
  167. }
  168. while (1) {
  169. c = getopt_long (argc, argv, "hVF:e:a:v:c:w:l:u:", longopts,
  170. &option);
  171. if (c == -1 || c == EOF)
  172. break;
  173. switch (c) {
  174. case 'F': /* input file */
  175. log_file = optarg;
  176. break;
  177. case 'e': /* ups name */
  178. expire_minutes = atoi (optarg);
  179. break;
  180. case 'a': /* port */
  181. if (!strcmp (optarg, "MAX"))
  182. use_average = FALSE;
  183. else
  184. use_average = TRUE;
  185. break;
  186. case 'v':
  187. variable_number = atoi (optarg);
  188. if (variable_number < 1 || variable_number > 2)
  189. usage4 (_("Invalid variable number"));
  190. break;
  191. case 'w': /* critical time threshold */
  192. value_warning_threshold = strtoul (optarg, NULL, 10);
  193. break;
  194. case 'c': /* warning time threshold */
  195. value_critical_threshold = strtoul (optarg, NULL, 10);
  196. break;
  197. case 'l': /* label */
  198. label = optarg;
  199. break;
  200. case 'u': /* timeout */
  201. units = optarg;
  202. break;
  203. case 'V': /* version */
  204. print_revision (progname, revision);
  205. exit (STATE_OK);
  206. case 'h': /* help */
  207. print_help ();
  208. exit (STATE_OK);
  209. case '?': /* help */
  210. usage5 ();
  211. }
  212. }
  213. c = optind;
  214. if (log_file == NULL && argc > c) {
  215. log_file = argv[c++];
  216. }
  217. if (expire_minutes <= 0 && argc > c) {
  218. if (is_intpos (argv[c]))
  219. expire_minutes = atoi (argv[c++]);
  220. else
  221. die (STATE_UNKNOWN,
  222. _("%s is not a valid expiration time\nUse '%s -h' for additional help\n"),
  223. argv[c], progname);
  224. }
  225. if (argc > c && strcmp (argv[c], "MAX") == 0) {
  226. use_average = FALSE;
  227. c++;
  228. }
  229. else if (argc > c && strcmp (argv[c], "AVG") == 0) {
  230. use_average = TRUE;
  231. c++;
  232. }
  233. if (argc > c && variable_number == -1) {
  234. variable_number = atoi (argv[c++]);
  235. if (variable_number < 1 || variable_number > 2) {
  236. printf ("%s :", argv[c]);
  237. usage (_("Invalid variable number\n"));
  238. }
  239. }
  240. if (argc > c && value_warning_threshold == 0) {
  241. value_warning_threshold = strtoul (argv[c++], NULL, 10);
  242. }
  243. if (argc > c && value_critical_threshold == 0) {
  244. value_critical_threshold = strtoul (argv[c++], NULL, 10);
  245. }
  246. if (argc > c && strlen (label) == 0) {
  247. label = argv[c++];
  248. }
  249. if (argc > c && strlen (units) == 0) {
  250. units = argv[c++];
  251. }
  252. return validate_arguments ();
  253. }
  254. int
  255. validate_arguments (void)
  256. {
  257. if (variable_number == -1)
  258. usage4 (_("You must supply the variable number"));
  259. if (label == NULL)
  260. label = strdup ("value");
  261. if (units == NULL)
  262. units = strdup ("");
  263. return OK;
  264. }
  265. void
  266. print_help (void)
  267. {
  268. print_revision (progname, revision);
  269. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  270. printf (COPYRIGHT, copyright, email);
  271. printf ("%s\n", _("This plugin will check either the average or maximum value of one of the"));
  272. printf ("%s\n", _("two variables recorded in an MRTG log file."));
  273. printf ("\n\n");
  274. print_usage ();
  275. printf (_(UT_HELP_VRSN));
  276. printf (" %s\n", "-F, --logfile=FILE");
  277. printf (" %s\n", _("The MRTG log file containing the data you want to monitor"));
  278. printf ("-e, --expires=MINUTES");
  279. printf (" %s\n", _("Minutes before MRTG data is considered to be too old"));
  280. printf (" %s\n", "-a, --aggregation=AVG|MAX");
  281. printf (" %s\n", _("Should we check average or maximum values?"));
  282. printf (" %s\n", "-v, --variable=INTEGER");
  283. printf (" %s\n", _("Which variable set should we inspect? (1 or 2)"));
  284. printf (" %s\n", "-w, --warning=INTEGER");
  285. printf (" %s\n", _("Threshold value for data to result in WARNING status"));
  286. printf (" %s\n", "-c, --critical=INTEGER");
  287. printf (" %s\n", _("Threshold value for data to result in CRITICAL status"));
  288. printf (" %s\n", "-l, --label=STRING");
  289. printf (" %s\n", _("Type label for data (Examples: Conns, \"Processor Load\", In, Out)"));
  290. printf (" %s\n", "-u, --units=STRING");
  291. printf (" %s\n", _("Option units label for data (Example: Packets/Sec, Errors/Sec,"));
  292. printf (" %s\n", _("\"Bytes Per Second\", \"%% Utilization\")"));
  293. printf ("\n");
  294. printf (" %s\n", _("If the value exceeds the <vwl> threshold, a WARNING status is returned. If"));
  295. printf (" %s\n", _("the value exceeds the <vcl> threshold, a CRITICAL status is returned. If"));
  296. printf (" %s\n", _("the data in the log file is older than <expire_minutes> old, a WARNING"));
  297. printf (" %s\n", _("status is returned and a warning message is printed."));
  298. printf ("\n");
  299. printf (" %s\n", _("This plugin is useful for monitoring MRTG data that does not correspond to"));
  300. printf (" %s\n", _("bandwidth usage. (Use the check_mrtgtraf plugin for monitoring bandwidth)."));
  301. printf (" %s\n", _("It can be used to monitor any kind of data that MRTG is monitoring - errors,"));
  302. printf (" %s\n", _("packets/sec, etc. I use MRTG in conjuction with the Novell NLM that allows"));
  303. printf (" %s\n", _("me to track processor utilization, user connections, drive space, etc and"));
  304. printf (" %s\n\n", _("this plugin works well for monitoring that kind of data as well."));
  305. printf ("%s\n", _("Notes:"));
  306. printf (" %s\n", _("- This plugin only monitors one of the two variables stored in the MRTG log"));
  307. printf (" %s\n", _("file. If you want to monitor both values you will have to define two"));
  308. printf (" %s\n", _("commands with different values for the <variable> argument. Of course,"));
  309. printf (" %s\n", _("you can always hack the code to make this plugin work for you..."));
  310. printf (" %s\n", _("- MRTG stands for the Multi Router Traffic Grapher. It can be downloaded from"));
  311. printf (" %s\n", "http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html");
  312. printf (_(UT_SUPPORT));
  313. }
  314. /* original command line:
  315. <log_file> <expire_minutes> <AVG|MAX> <variable> <vwl> <vcl> <label> [units] */
  316. void
  317. print_usage (void)
  318. {
  319. printf (_("Usage:"));
  320. printf ("%s -F log_file -a <AVG | MAX> -v variable -w warning -c critical\n",progname);
  321. printf ("[-l label] [-u units] [-e expire_minutes] [-t timeout] [-v]\n");
  322. }