diff --git a/plugins/check_vsz.c b/plugins/check_vsz.c
index f53eeae370062e8076a12a2ccb3ec4409d8af7b3..7a9acb2dfea05021b6fa639f56a90f5bdfe7264e 100644 (file)
--- a/plugins/check_vsz.c
+++ b/plugins/check_vsz.c
*
*****************************************************************************/
+const char *progname = "check_vsz";
+#define REVISION "$Revision$"
+#define COPYRIGHT "1999-2002"
+#define AUTHOR "Karl DeBisschop"
+#define EMAIL "karl@debisschop.net"
+#define SUMMARY "Check the image size of a running program.\n"
+
#include "common.h"
#include "popen.h"
#include "utils.h"
int process_arguments (int argc, char **argv);
-int call_getopt (int argc, char **argv);
-void print_help (char *cmd);
-void print_usage (char *cmd);
+void print_help (const char *cmd);
+void print_usage (const char *cmd);
int warn = -1;
int crit = -1;
int proc_size = -1;
char input_buffer[MAX_INPUT_BUFFER];
char proc_name[MAX_INPUT_BUFFER];
- char *message = NULL;
+ char *message = "";
- if (!process_arguments (argc, argv)) {
- printf ("%s: failure parsing arguments\n", my_basename (argv[0]));
- print_help (my_basename (argv[0]));
+ if (process_arguments (argc, argv) == ERROR) {
+ printf ("%s: failure parsing arguments\n", progname);
+ print_help (progname);
return STATE_UNKNOWN;
}
if (child_stderr == NULL)
printf ("Could not open stderr for %s\n", VSZ_COMMAND);
- message = malloc ((size_t) 1);
- message[0] = 0;
while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
line++;
if (sscanf (input_buffer, VSZ_FORMAT, &proc_size, proc_name) == 2) {
if (proc == NULL) {
if (proc_size > warn) {
- len = strlen (message) + strlen (proc_name) + 23;
- message = realloc (message, len);
- if (message == NULL)
- terminate (STATE_UNKNOWN,
- "check_vsz: could not malloc message (1)");
- sprintf (message, "%s %s(%d)", message, proc_name, proc_size);
+ asprintf (&message, "%s %s(%d)", message, proc_name, proc_size);
result = max_state (result, STATE_WARNING);
}
if (proc_size > crit) {
}
}
else if (strstr (proc_name, proc)) {
- len = strlen (message) + 21;
- message = realloc (message, len);
- if (message == NULL)
- terminate (STATE_UNKNOWN,
- "check_vsz: could not malloc message (2)");
- sprintf (message, "%s %d", message, proc_size);
+ asprintf (&message, "%s %d", message, proc_size);
if (proc_size > warn) {
result = max_state (result, STATE_WARNING);
}
{
int c;
- if (argc < 2)
- return ERROR;
-
- c = 0;
- while (c += (call_getopt (argc - c, &argv[c]))) {
- if (argc <= c)
- break;
- if (warn == -1) {
- if (!is_intnonneg (argv[c])) {
- printf ("%s: critical threshold must be an integer: %s\n",
- my_basename (argv[0]), argv[c]);
- print_usage (my_basename (argv[0]));
- exit (STATE_UNKNOWN);
- }
- warn = atoi (argv[c]);
- }
- else if (crit == -1) {
- if (!is_intnonneg (argv[c])) {
- printf ("%s: critical threshold must be an integer: %s\n",
- my_basename (argv[0]), argv[c]);
- print_usage (my_basename (argv[0]));
- exit (STATE_UNKNOWN);
- }
- crit = atoi (argv[c]);
- }
- else if (proc == NULL) {
- proc = malloc (strlen (argv[c]) + 1);
- if (proc == NULL)
- terminate (STATE_UNKNOWN,
- "check_vsz: failed malloc of proc in process_arguments");
- strcpy (proc, argv[c]);
- }
- }
- return c;
-}
-
-int
-call_getopt (int argc, char **argv)
-{
- int c, i = 1;
-
-#ifdef HAVE_GETOPT_H
int option_index = 0;
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"command", required_argument, 0, 'C'},
{0, 0, 0, 0}
};
-#endif
+
+ if (argc < 2)
+ return ERROR;
while (1) {
-#ifdef HAVE_GETOPT_H
c = getopt_long (argc, argv, "+hVc:w:C:", long_options, &option_index);
-#else
- c = getopt (argc, argv, "+hVc:w:C:");
-#endif
+
if (c == EOF)
break;
- i++;
- switch (c) {
- case 'c':
- case 'w':
- case 'C':
- i++;
- }
-
switch (c) {
case '?': /* help */
- printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
- print_usage (my_basename (argv[0]));
+ print_usage (progname);
exit (STATE_UNKNOWN);
case 'h': /* help */
- print_help (my_basename (argv[0]));
+ print_help (progname);
exit (STATE_OK);
case 'V': /* version */
- print_revision (my_basename (argv[0]), "$Revision$");
+ print_revision (progname, "$Revision$");
exit (STATE_OK);
case 'c': /* critical threshold */
if (!is_intnonneg (optarg)) {
printf ("%s: critical threshold must be an integer: %s\n",
- my_basename (argv[0]), optarg);
- print_usage (my_basename (argv[0]));
+ progname, optarg);
+ print_usage (progname);
exit (STATE_UNKNOWN);
}
crit = atoi (optarg);
case 'w': /* warning threshold */
if (!is_intnonneg (optarg)) {
printf ("%s: warning threshold must be an integer: %s\n",
- my_basename (argv[0]), optarg);
- print_usage (my_basename (argv[0]));
+ progname, optarg);
+ print_usage (progname);
exit (STATE_UNKNOWN);
}
warn = atoi (optarg);
break;
case 'C': /* command name */
- proc = malloc (strlen (optarg) + 1);
- if (proc == NULL)
- terminate (STATE_UNKNOWN,
- "check_vsz: failed malloc of proc in process_arguments");
- strcpy (proc, optarg);
+ proc = optarg;
break;
}
}
- return i;
+
+ c = optind;
+ if (warn == -1) {
+ if (!is_intnonneg (argv[c])) {
+ printf ("%s: critical threshold must be an integer: %s\n",
+ progname, argv[c]);
+ print_usage (progname);
+ exit (STATE_UNKNOWN);
+ }
+ warn = atoi (argv[c++]);
+ }
+
+ if (crit == -1) {
+ if (!is_intnonneg (argv[c])) {
+ printf ("%s: critical threshold must be an integer: %s\n",
+ progname, argv[c]);
+ print_usage (progname);
+ exit (STATE_UNKNOWN);
+ }
+ crit = atoi (argv[c++]);
+ }
+
+ if (proc == NULL)
+ proc = argv[c];
+
+ return c;
}
void
-print_usage (char *cmd)
+print_usage (const char *cmd)
{
printf ("Usage: %s -w <wsize> -c <csize> [-C command]\n"
" %s --help\n" " %s --version\n", cmd, cmd, cmd);
}
void
-print_help (char *cmd)
+print_help (const char *cmd)
{
print_revision ("check_vsz", "$Revision$");
printf