check_mrtgtraf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*****************************************************************************
  2. *
  3. * Nagios check_mrtgtraf plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2014 Nagios Plugins Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains the check_mtrgtraf plugin
  11. *
  12. * This plugin will check the incoming/outgoing transfer rates of a router
  13. * switch, etc recorded in an MRTG log.
  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. #include "common.h"
  32. #include "utils.h"
  33. const char *progname = "check_mrtgtraf";
  34. const char *copyright = "1999-2014";
  35. const char *email = "devel@nagios-plugins.org";
  36. int process_arguments (int, char **);
  37. int validate_arguments (void);
  38. void print_help(void);
  39. void print_usage(void);
  40. int verbose = false;
  41. char *log_file = NULL;
  42. int expire_minutes = -1;
  43. int use_average = TRUE;
  44. unsigned long incoming_warning_threshold = 0L;
  45. unsigned long incoming_critical_threshold = 0L;
  46. unsigned long outgoing_warning_threshold = 0L;
  47. unsigned long outgoing_critical_threshold = 0L;
  48. int
  49. main (int argc, char **argv)
  50. {
  51. int result = STATE_OK;
  52. FILE *fp;
  53. int line;
  54. char input_buffer[MAX_INPUT_BUFFER];
  55. char *temp_buffer;
  56. time_t current_time;
  57. char *error_message;
  58. time_t timestamp = 0L;
  59. unsigned long average_incoming_rate = 0L;
  60. unsigned long average_outgoing_rate = 0L;
  61. unsigned long maximum_incoming_rate = 0L;
  62. unsigned long maximum_outgoing_rate = 0L;
  63. unsigned long incoming_rate = 0L;
  64. unsigned long outgoing_rate = 0L;
  65. double adjusted_incoming_rate = 0.0;
  66. double adjusted_outgoing_rate = 0.0;
  67. char incoming_speed_rating[8];
  68. char outgoing_speed_rating[8];
  69. setlocale (LC_ALL, "");
  70. bindtextdomain (PACKAGE, LOCALEDIR);
  71. textdomain (PACKAGE);
  72. /* Parse extra opts if any */
  73. argv=np_extra_opts (&argc, argv, progname);
  74. if (process_arguments (argc, argv) == ERROR)
  75. usage4 (_("Could not parse arguments"));
  76. /* open the MRTG log file for reading */
  77. fp = fopen (log_file, "r");
  78. if (fp == NULL)
  79. usage4 (_("Unable to open MRTG log file"));
  80. line = 0;
  81. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  82. line++;
  83. /* skip the first line of the log file */
  84. if (line == 1)
  85. continue;
  86. /* break out of read loop */
  87. /* if we've passed the number of entries we want to read */
  88. if (line > 2)
  89. break;
  90. /* grab the timestamp */
  91. temp_buffer = strtok (input_buffer, " ");
  92. timestamp = strtoul (temp_buffer, NULL, 10);
  93. if (verbose)
  94. printf("%s %lu\n", _("Found timestamp of:"), timestamp);
  95. /* grab the average incoming transfer rate */
  96. temp_buffer = strtok (NULL, " ");
  97. average_incoming_rate = strtoul (temp_buffer, NULL, 10);
  98. if (verbose)
  99. printf("%s %lu\n", _("Found average incoming rate of:"), average_incoming_rate);
  100. /* grab the average outgoing transfer rate */
  101. temp_buffer = strtok (NULL, " ");
  102. average_outgoing_rate = strtoul (temp_buffer, NULL, 10);
  103. if (verbose)
  104. printf("%s %lu\n", _("Found average outgoing rate of:"), average_outgoing_rate);
  105. /* grab the maximum incoming transfer rate */
  106. temp_buffer = strtok (NULL, " ");
  107. maximum_incoming_rate = strtoul (temp_buffer, NULL, 10);
  108. if (verbose)
  109. printf("%s %lu\n", _("Found maximum incoming rate of:"), maximum_incoming_rate);
  110. /* grab the maximum outgoing transfer rate */
  111. temp_buffer = strtok (NULL, " ");
  112. maximum_outgoing_rate = strtoul (temp_buffer, NULL, 10);
  113. if (verbose)
  114. printf("%s %lu\n", _("Found maximum outgoing rate of:"), maximum_outgoing_rate);
  115. }
  116. /* close the log file */
  117. fclose (fp);
  118. /* if we couldn't read enough data, return an unknown error */
  119. if (line <= 2)
  120. usage4 (_("Unable to process MRTG log file"));
  121. /* make sure the MRTG data isn't too old */
  122. time (&current_time);
  123. if ((expire_minutes > 0) &&
  124. (current_time - timestamp) > (expire_minutes * 60))
  125. die (STATE_WARNING, _("MRTG data has expired (%d minutes old)\n"),
  126. (int) ((current_time - timestamp) / 60));
  127. /* else check the incoming/outgoing rates */
  128. if (use_average == TRUE) {
  129. if (verbose) printf("%s\n", _("Using average rates not maximum."));
  130. incoming_rate = average_incoming_rate;
  131. outgoing_rate = average_outgoing_rate;
  132. }
  133. else {
  134. if (verbose) printf("%s\n", _("Using default maximum rates."));
  135. incoming_rate = maximum_incoming_rate;
  136. outgoing_rate = maximum_outgoing_rate;
  137. }
  138. /* report incoming traffic in Bytes/sec */
  139. if (incoming_rate < 1024) {
  140. strcpy (incoming_speed_rating, "B/s");
  141. adjusted_incoming_rate = (double) incoming_rate;
  142. }
  143. /* report incoming traffic in KBytes/sec */
  144. else if (incoming_rate < (1024 * 1024)) {
  145. strcpy (incoming_speed_rating, "KB/s");
  146. adjusted_incoming_rate = (double) (incoming_rate / 1024.0);
  147. }
  148. /* report incoming traffic in MBytes/sec */
  149. else {
  150. strcpy (incoming_speed_rating, "MB/s");
  151. adjusted_incoming_rate = (double) (incoming_rate / 1024.0 / 1024.0);
  152. }
  153. /* report outgoing traffic in Bytes/sec */
  154. if (outgoing_rate < 1024) {
  155. strcpy (outgoing_speed_rating, "B/s");
  156. adjusted_outgoing_rate = (double) outgoing_rate;
  157. }
  158. /* report outgoing traffic in KBytes/sec */
  159. else if (outgoing_rate < (1024 * 1024)) {
  160. strcpy (outgoing_speed_rating, "KB/s");
  161. adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0);
  162. }
  163. /* report outgoing traffic in MBytes/sec */
  164. else {
  165. strcpy (outgoing_speed_rating, "MB/s");
  166. adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0 / 1024.0);
  167. }
  168. if (incoming_rate > incoming_critical_threshold
  169. || outgoing_rate > outgoing_critical_threshold) {
  170. result = STATE_CRITICAL;
  171. }
  172. else if (incoming_rate > incoming_warning_threshold
  173. || outgoing_rate > outgoing_warning_threshold) {
  174. result = STATE_WARNING;
  175. }
  176. xasprintf (&error_message, _("%s. In = %0.1f %s, %s. Out = %0.1f %s|%s %s\n"),
  177. (use_average == TRUE) ? _("Avg") : _("Max"), adjusted_incoming_rate,
  178. incoming_speed_rating, (use_average == TRUE) ? _("Avg") : _("Max"),
  179. adjusted_outgoing_rate, outgoing_speed_rating,
  180. fperfdata("in", adjusted_incoming_rate, incoming_speed_rating,
  181. (int)incoming_warning_threshold, incoming_warning_threshold,
  182. (int)incoming_critical_threshold, incoming_critical_threshold,
  183. TRUE, 0, FALSE, 0),
  184. fperfdata("out", adjusted_outgoing_rate, outgoing_speed_rating,
  185. (int)outgoing_warning_threshold, outgoing_warning_threshold,
  186. (int)outgoing_critical_threshold, outgoing_critical_threshold,
  187. TRUE, 0, FALSE, 0));
  188. printf (_("Traffic %s - %s\n"), state_text(result), error_message);
  189. return result;
  190. }
  191. /* process command-line arguments */
  192. int
  193. process_arguments (int argc, char **argv)
  194. {
  195. int c;
  196. int option = 0;
  197. static struct option longopts[] = {
  198. {"filename", required_argument, 0, 'F'},
  199. {"expires", required_argument, 0, 'e'},
  200. {"aggregation", required_argument, 0, 'a'},
  201. {"critical", required_argument, 0, 'c'},
  202. {"warning", required_argument, 0, 'w'},
  203. {"verbose", no_argument, 0, 'v'},
  204. {"version", no_argument, 0, 'V'},
  205. {"help", no_argument, 0, 'h'},
  206. {0, 0, 0, 0}
  207. };
  208. if (argc < 2)
  209. return ERROR;
  210. for (c = 1; c < argc; c++) {
  211. if (strcmp ("-to", argv[c]) == 0)
  212. strcpy (argv[c], "-t");
  213. else if (strcmp ("-wt", argv[c]) == 0)
  214. strcpy (argv[c], "-w");
  215. else if (strcmp ("-ct", argv[c]) == 0)
  216. strcpy (argv[c], "-c");
  217. }
  218. while (1) {
  219. c = getopt_long (argc, argv, "hVvF:e:a:c:w:", longopts, &option);
  220. if (c == -1 || c == EOF)
  221. break;
  222. switch (c) {
  223. case 'F': /* input file */
  224. log_file = optarg;
  225. break;
  226. case 'e': /* expiration time */
  227. expire_minutes = atoi (optarg);
  228. break;
  229. case 'a': /* aggregation (AVE or MAX) */
  230. if (!strcmp (optarg, "MAX"))
  231. use_average = FALSE;
  232. else
  233. use_average = TRUE;
  234. break;
  235. case 'c': /* warning threshold */
  236. sscanf (optarg, "%lu,%lu", &incoming_critical_threshold,
  237. &outgoing_critical_threshold);
  238. break;
  239. case 'w': /* critical threshold */
  240. sscanf (optarg, "%lu,%lu", &incoming_warning_threshold,
  241. &outgoing_warning_threshold);
  242. break;
  243. case 'v':
  244. verbose = true;
  245. break;
  246. case 'V': /* version */
  247. print_revision (progname, NP_VERSION);
  248. exit (STATE_OK);
  249. case 'h': /* help */
  250. print_help ();
  251. exit (STATE_OK);
  252. case '?': /* help */
  253. usage5 ();
  254. }
  255. }
  256. c = optind;
  257. if (argc > c && log_file == NULL) {
  258. log_file = argv[c++];
  259. }
  260. if (argc > c && expire_minutes == -1) {
  261. expire_minutes = atoi (argv[c++]);
  262. }
  263. if (argc > c && strcmp (argv[c], "MAX") == 0) {
  264. use_average = FALSE;
  265. c++;
  266. }
  267. else if (argc > c && strcmp (argv[c], "AVG") == 0) {
  268. use_average = TRUE;
  269. c++;
  270. }
  271. if (argc > c && incoming_warning_threshold == 0) {
  272. incoming_warning_threshold = strtoul (argv[c++], NULL, 10);
  273. }
  274. if (argc > c && incoming_critical_threshold == 0) {
  275. incoming_critical_threshold = strtoul (argv[c++], NULL, 10);
  276. }
  277. if (argc > c && outgoing_warning_threshold == 0) {
  278. outgoing_warning_threshold = strtoul (argv[c++], NULL, 10);
  279. }
  280. if (argc > c && outgoing_critical_threshold == 0) {
  281. outgoing_critical_threshold = strtoul (argv[c++], NULL, 10);
  282. }
  283. return validate_arguments ();
  284. }
  285. int
  286. validate_arguments (void)
  287. {
  288. return OK;
  289. }
  290. void
  291. print_help (void)
  292. {
  293. print_revision (progname, NP_VERSION);
  294. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  295. printf (COPYRIGHT, copyright, email);
  296. printf ("%s\n", _("This plugin will check the incoming/outgoing transfer rates of a router,"));
  297. printf ("%s\n", _("switch, etc recorded in an MRTG log. If the newest log entry is older"));
  298. printf ("%s\n", _("than <expire_minutes>, a WARNING status is returned. If either the"));
  299. printf ("%s\n", _("incoming or outgoing rates exceed the <icl> or <ocl> thresholds (in"));
  300. printf ("%s\n", _("Bytes/sec), a CRITICAL status results. If either of the rates exceed"));
  301. printf ("%s\n", _("the <iwl> or <owl> thresholds (in Bytes/sec), a WARNING status results."));
  302. printf ("\n\n");
  303. print_usage ();
  304. printf (UT_HELP_VRSN);
  305. printf (UT_EXTRA_OPTS);
  306. printf (" %s\n", "-F, --filename=STRING");
  307. printf (" %s\n", _("File to read log from"));
  308. printf (" %s\n", "-e, --expires=INTEGER");
  309. printf (" %s\n", _("Minutes after which log expires"));
  310. printf (" %s\n", "-a, --aggregation=(AVG|MAX)");
  311. printf (" %s\n", _("Test average or maximum"));
  312. printf (" %s\n", "-w, --warning");
  313. printf (" %s\n", _("Warning threshold pair <incoming>,<outgoing>"));
  314. printf (" %s\n", "-c, --critical");
  315. printf (" %s\n", _("Critical threshold pair <incoming>,<outgoing>"));
  316. printf (" %s\n", _("-v, --verbose"));
  317. printf (" %s\n", _("Verbose output durring plugin runtime."));
  318. printf ("\n");
  319. printf ("%s\n", _("Notes:"));
  320. printf (" %s\n", _("- MRTG stands for Multi Router Traffic Grapher. It can be downloaded from"));
  321. printf (" %s\n", " http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html");
  322. printf (" %s\n", _("- While MRTG can monitor things other than traffic rates, this"));
  323. printf (" %s\n", _(" plugin probably won't work with much else without modification."));
  324. printf (" %s\n", _("- The calculated i/o rates are a little off from what MRTG actually"));
  325. printf (" %s\n", _(" reports. I'm not sure why this is right now, but will look into it"));
  326. printf (" %s\n", _(" for future enhancements of this plugin."));
  327. printf (UT_SUPPORT);
  328. }
  329. void
  330. print_usage (void)
  331. {
  332. printf (_("Usage"));
  333. printf (" %s -F <log_file> -a <AVG | MAX> -w <warning_pair>\n",progname);
  334. printf ("-c <critical_pair> [-e expire_minutes]\n");
  335. }