check_uptime.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*********************************************************************************
  2. *
  3. * Nagios check_uptime plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2013-2014 Nagios Plugin Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains the check_uptime plugin
  11. *
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. *********************************************************************************/
  27. #include "common.h"
  28. #include "utils.h"
  29. #include "utils_base.h"
  30. #include <time.h>
  31. char *progname = "check_uptime";
  32. char *version = "1.0";
  33. char *email = "devel@nagios-plugins.org";
  34. char *copyright = "2014";
  35. char *developer = "Andy Brist";
  36. static int process_arguments (int, char **);
  37. void print_help (void);
  38. void print_usage (void);
  39. int verbose = 0;
  40. char *warning, *critical, *timeunit;
  41. thresholds *my_thresholds = NULL;
  42. int main (int argc, char **argv) {
  43. // base values
  44. int status, intvalue;
  45. int upminutes, uphours, updays;
  46. double value, uptime;
  47. char* perf;
  48. char* output_message;
  49. /* Parse extra opts if any */
  50. argv = np_extra_opts (&argc, argv, progname);
  51. if (process_arguments (argc, argv) == ERROR)
  52. usage4 (_("Could not parse arguments"));
  53. /* Set signal handling and alarm timeout */
  54. if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
  55. die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));
  56. }
  57. alarm (timeout_interval);
  58. value = getuptime();
  59. if (verbose >= 3) {
  60. printf("Uptime in seconds returned from timespec struct: %f\n", value);
  61. }
  62. intvalue = (int)value;
  63. updays = intvalue / 86400;
  64. uphours = (intvalue % 86400) / 3600;
  65. upminutes = ((intvalue % 86400) % 3600) / 60;
  66. if (!strncmp(timeunit, "minutes", strlen("minutes"))) {
  67. uptime = intvalue / 60;
  68. } else if (!strncmp(timeunit, "hours", strlen("hours"))) {
  69. uptime = intvalue / 3600;
  70. } else if (!strncmp(timeunit, "days", strlen("days"))) {
  71. uptime = intvalue / 86400;
  72. } else {
  73. uptime = intvalue;
  74. }
  75. xasprintf(&output_message,_("%u day(s) %u hour(s) %u minute(s)"), updays, uphours, upminutes);
  76. xasprintf(&perf,_("%s"),
  77. fperfdata("uptime", uptime, "",
  78. my_thresholds->warning?TRUE:FALSE, my_thresholds->warning?my_thresholds->warning->end:0,
  79. my_thresholds->critical?TRUE:FALSE, my_thresholds->critical?my_thresholds->critical->end:0,
  80. FALSE, 0,
  81. FALSE, 0)
  82. );
  83. status = get_status(uptime, my_thresholds);
  84. if (status == STATE_OK) {
  85. printf("Uptime %s: %s | %s\n", _("OK"), output_message, perf);
  86. } else if (status == STATE_WARNING) {
  87. printf("Uptime %s: %s | %s\n", _("WARNING"), output_message, perf);
  88. } else if (status == STATE_CRITICAL) {
  89. printf("Uptime %s: %s | %s\n", _("CRITICAL"), output_message, perf);
  90. }
  91. return status;
  92. } // end main
  93. int getuptime () {
  94. struct timespec t;
  95. clock_gettime(CLOCK_MONOTONIC, &t);
  96. if (t.tv_sec > 0) {
  97. return t.tv_sec;
  98. } else {
  99. printf("Uptime UNKNOWN: Timespec struct failed to retrieve uptime\n");
  100. exit (STATE_UNKNOWN);
  101. }
  102. } // end getuptime
  103. static int process_arguments (int argc, char **argv) {
  104. int c;
  105. int escape = 0;
  106. char *temp;
  107. int option = 0;
  108. static struct option longopts[] = {
  109. {"critical", required_argument, 0, 'c'},
  110. {"warning", required_argument, 0, 'w'},
  111. {"timeout", required_argument, 0, 't'},
  112. {"timeunit", required_argument, 0, 'u'},
  113. {"verbose", no_argument, 0, 'v'},
  114. {"version", no_argument, 0, 'V'},
  115. {"help", no_argument, 0, 'h'},
  116. {0, 0, 0, 0}
  117. };
  118. while ( 1 ) {
  119. c = getopt_long ( argc, argv, "+hvVu:c:w:t:", longopts, &option );
  120. if ( c == -1 || c == EOF || c == 1 ) break;
  121. switch ( c ) {
  122. case '?':
  123. usage5 ();
  124. case 'h':
  125. print_help ();
  126. exit ( STATE_OK );
  127. case 'v':
  128. verbose++;
  129. if (verbose >= 3) {
  130. printf("Verbose mode enabled\n");
  131. }
  132. break;
  133. case 'V':
  134. print_revision (progname, NP_VERSION);
  135. exit (STATE_OK);
  136. case 'u':
  137. timeunit = optarg;
  138. break;
  139. case 'c':
  140. critical = optarg;
  141. break;
  142. case 'w':
  143. warning = optarg;
  144. break;
  145. case 't': /* timeout period */
  146. timeout_interval = parse_timeout_string (optarg);
  147. break;
  148. } // end case
  149. } // end while
  150. c = optind;
  151. set_thresholds(&my_thresholds, warning, critical);
  152. return validate_arguments ();
  153. } // end process_arguments
  154. int validate_arguments (void) {
  155. if (timeunit == NULL) {
  156. timeunit = "minutes";
  157. if (verbose >= 3) {
  158. printf("No unit of time measurement specified. Using default: \"minutes\"\n");
  159. }
  160. } else if (strncmp(timeunit, "seconds", strlen("seconds") + 1 ) &&
  161. strncmp(timeunit, "minutes", strlen("minutes") + 1) &&
  162. strncmp(timeunit, "hours", strlen("hours") + 1) &&
  163. strncmp(timeunit, "days", strlen("days") + 1)) {
  164. if (verbose >= 3) {
  165. printf("Invalid unit of time measurement specified: \"%s\"\n", timeunit);
  166. }
  167. usage4(_("Wrong -u argument, expected: seconds, minutes, hours, or days"));
  168. } else if (verbose >= 3) {
  169. printf("Specified unit of time measurement accepted: \"%s\"\n", timeunit);
  170. }
  171. return OK;
  172. } //end validate
  173. void print_usage (void) {
  174. printf( "%s\n", _("Usage:") );
  175. printf( "%s", _("check_uptime ") );
  176. printf( "%s\n", _("[-u uom] [-w threshold] [-c threshold] [-t] [-h] [-vvv] [-V]") );
  177. } // end usage
  178. void print_help (void) {
  179. print_revision ( progname, NP_VERSION );
  180. printf ( COPYRIGHT, copyright, developer, email );
  181. printf ( "%s\n", _("This plugin checks the system uptime and alerts if less than the threshold.") );
  182. printf ( "%s\n", _("Threshold unit of measurement specified with \"-u\".") );
  183. printf ( "%s\n", _("\"-u\" switch supports: seconds|minutes|hours|days.") );
  184. print_usage ();
  185. printf ( UT_HELP_VRSN );
  186. printf ( UT_EXTRA_OPTS );
  187. printf ( "%s\n", _("-t, Plugin timeout, default 10 seconds") );
  188. printf ( "%s\n", _("-c, Critcal threshold") );
  189. printf ( "%s\n", _("-w, Warning threshold") );
  190. printf ( "%s\n", _("-u, Time unit of measurement (seconds|minutes|hours|days) (default: minutes)") );
  191. printf ( "%s\n", _("-vvv, Enable verbose output") );
  192. //printf ( "%s\n", _("-h, Print help and usage") );
  193. printf ( UT_SUPPORT );
  194. } // end print_help