diff --git a/plugins/disk/smart-c/Makefile b/plugins/disk/smart-c/Makefile new file mode 100644 index 00000000..f1a52a82 --- /dev/null +++ b/plugins/disk/smart-c/Makefile @@ -0,0 +1,6 @@ +smart_: smart_.o common.o + +.PHONY: clean + +clean: + rm -f smart_ smart_.o common.o diff --git a/plugins/disk/smart-c/common.c b/plugins/disk/smart-c/common.c new file mode 100644 index 00000000..39ba514d --- /dev/null +++ b/plugins/disk/smart-c/common.c @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2008-2013 Helmut Grohne - All rights reserved. + * + * This copyrighted material is made available to anyone wishing to use, + * modify, copy, or redistribute it subject to the terms and conditions + * of the GNU General Public License v.2 or v.3. + */ +#include +#include +#include +#include +#include +#include "common.h" + +extern char **environ; + +int writeyes(void) +{ + puts("yes"); + return 0; +} + +int autoconf_check_readable(const char *path) +{ + if (0 == access(path, R_OK)) + return writeyes(); + else { + printf("no (%s is not readable, errno=%d)\n", path, errno); + return 0; + } +} + +int getenvint(const char *name, int defvalue) +{ + const char *value; + value = getenv(name); + if (value == NULL) + return defvalue; + return atoi(value); +} + +static + /*@null@ */ + /*@observer@ */ +const char *getenv_composed(const char *name1, const char *name2) +{ + char **p; + size_t len1 = strlen(name1), len2 = strlen(name2); + for (p = environ; *p; ++p) { + if (0 == strncmp(*p, name1, len1) && + 0 == strncmp(len1 + *p, name2, len2) && + (*p)[len1 + len2] == '=') + return len1 + len2 + 1 + *p; + } + return NULL; +} + +void print_warning(const char *name) +{ + const char *p; + p = getenv_composed(name, "_warning"); + if (p == NULL) + p = getenv("warning"); + if (p == NULL) + return; + + printf("%s.warning %s\n", name, p); +} + +void print_critical(const char *name) +{ + const char *p; + p = getenv_composed(name, "_critical"); + if (p == NULL) + p = getenv("critical"); + if (p == NULL) + return; + + printf("%s.critical %s\n", name, p); +} + +void print_warncrit(const char *name) +{ + print_warning(name); + print_critical(name); +} + +int fail(const char *message) +{ + fputs(message, stderr); + fputc('\n', stderr); + return 1; +} diff --git a/plugins/disk/smart-c/common.h b/plugins/disk/smart-c/common.h new file mode 100644 index 00000000..34ca0541 --- /dev/null +++ b/plugins/disk/smart-c/common.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2008 Helmut Grohne - All rights reserved. + * + * This copyrighted material is made available to anyone wishing to use, + * modify, copy, or redistribute it subject to the terms and conditions + * of the GNU General Public License v.2 or v.3. + */ +#ifndef COMMON_H +#define COMMON_H + +#define PROC_STAT "/proc/stat" + +/** Write yes to stdout and return 0. The intended use is give an autoconf + * response like "return writeyes();". + * @returns a success state to be passed on as the return value from main */ +int writeyes(void); + +/** Answer an autoconf request by checking the readability of the given file. + */ +int autoconf_check_readable(const char *); + +/** Obtain an integer value from the environment. In the absence of the + * variable the given defaultvalue is returned. */ +int getenvint(const char *, int defaultvalue); + +/** Print a name.warning line using the "name_warning" or "warning" environment + * variables. */ +void print_warning(const char *name); + +/** Print a name.critical line using the "name_critical" or "critical" + * environment variables. */ +void print_critical(const char *name); + +/** Print both name.warning and name.critical lines using environment + * variables. */ +void print_warncrit(const char *name); + +/** Fail by printing the given message and a newline to stderr. + * @returns a failure state to be passed on as the return value from main */ +int fail(const char *message); + +#define xisspace(x) isspace((int)(unsigned char) x) +#define xisdigit(x) isdigit((int)(unsigned char) x) + +#endif diff --git a/plugins/disk/smart-c/smart_.c b/plugins/disk/smart-c/smart_.c index ed057360..a474259b 100644 --- a/plugins/disk/smart-c/smart_.c +++ b/plugins/disk/smart-c/smart_.c @@ -68,7 +68,7 @@ static char getitem(char *input, unsigned char item, char *output) return 0; } -int smart(int argc, char **argv) +int main(int argc, char **argv) { char command[50]; char output[255];