1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-21 18:41:03 +00:00

smart: importing common.{h,c} from munin-c

The compilation is very easy, a simple `make` should work, as I provided
a convenient `Makefile`
This commit is contained in:
Steve Schnepp 2021-02-07 12:05:40 +01:00
parent a420c4eb58
commit 6eff786eef
4 changed files with 145 additions and 1 deletions

View file

@ -0,0 +1,6 @@
smart_: smart_.o common.o
.PHONY: clean
clean:
rm -f smart_ smart_.o common.o

View file

@ -0,0 +1,93 @@
/*
* Copyright (C) 2008-2013 Helmut Grohne <helmut@subdivi.de> - 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 <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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;
}

View file

@ -0,0 +1,45 @@
/*
* Copyright (C) 2008 Helmut Grohne <helmut@subdivi.de> - 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

View file

@ -68,7 +68,7 @@ static char getitem(char *input, unsigned char item, char *output)
return 0; return 0;
} }
int smart(int argc, char **argv) int main(int argc, char **argv)
{ {
char command[50]; char command[50];
char output[255]; char output[255];