check_mrtgtraf.c 13 KB

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