Projet

Général

Profil

Révision 6eff786e

ID6eff786eeff513814e0b205b00a4b97423e308b8
Parent a420c4eb
Enfant b86f1d0f

Ajouté par Steve Schnepp il y a presque 5 ans

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

The compilation is very easy, a simple `make` should work, as I provided
a convenient `Makefile`

Voir les différences:

plugins/disk/smart-c/Makefile
1
smart_: smart_.o common.o
2

  
3
.PHONY: clean
4

  
5
clean:
6
	rm -f smart_ smart_.o common.o
plugins/disk/smart-c/common.c
1
/*
2
 * Copyright (C) 2008-2013 Helmut Grohne <helmut@subdivi.de> - All rights reserved.
3
 *
4
 * This copyrighted material is made available to anyone wishing to use,
5
 * modify, copy, or redistribute it subject to the terms and conditions
6
 * of the GNU General Public License v.2 or v.3.
7
 */
8
#include <errno.h>
9
#include <stdio.h>
10
#include <stdlib.h>
11
#include <string.h>
12
#include <unistd.h>
13
#include "common.h"
14

  
15
extern char **environ;
16

  
17
int writeyes(void)
18
{
19
	puts("yes");
20
	return 0;
21
}
22

  
23
int autoconf_check_readable(const char *path)
24
{
25
	if (0 == access(path, R_OK))
26
		return writeyes();
27
	else {
28
		printf("no (%s is not readable, errno=%d)\n", path, errno);
29
		return 0;
30
	}
31
}
32

  
33
int getenvint(const char *name, int defvalue)
34
{
35
	const char *value;
36
	value = getenv(name);
37
	if (value == NULL)
38
		return defvalue;
39
	return atoi(value);
40
}
41

  
42
static
43
				    /*@null@ */
44
 /*@observer@ */
45
const char *getenv_composed(const char *name1, const char *name2)
46
{
47
	char **p;
48
	size_t len1 = strlen(name1), len2 = strlen(name2);
49
	for (p = environ; *p; ++p) {
50
		if (0 == strncmp(*p, name1, len1) &&
51
		    0 == strncmp(len1 + *p, name2, len2) &&
52
		    (*p)[len1 + len2] == '=')
53
			return len1 + len2 + 1 + *p;
54
	}
55
	return NULL;
56
}
57

  
58
void print_warning(const char *name)
59
{
60
	const char *p;
61
	p = getenv_composed(name, "_warning");
62
	if (p == NULL)
63
		p = getenv("warning");
64
	if (p == NULL)
65
		return;
66

  
67
	printf("%s.warning %s\n", name, p);
68
}
69

  
70
void print_critical(const char *name)
71
{
72
	const char *p;
73
	p = getenv_composed(name, "_critical");
74
	if (p == NULL)
75
		p = getenv("critical");
76
	if (p == NULL)
77
		return;
78

  
79
	printf("%s.critical %s\n", name, p);
80
}
81

  
82
void print_warncrit(const char *name)
83
{
84
	print_warning(name);
85
	print_critical(name);
86
}
87

  
88
int fail(const char *message)
89
{
90
	fputs(message, stderr);
91
	fputc('\n', stderr);
92
	return 1;
93
}
plugins/disk/smart-c/common.h
1
/*
2
 * Copyright (C) 2008 Helmut Grohne <helmut@subdivi.de> - All rights reserved.
3
 *
4
 * This copyrighted material is made available to anyone wishing to use,
5
 * modify, copy, or redistribute it subject to the terms and conditions
6
 * of the GNU General Public License v.2 or v.3.
7
 */
8
#ifndef COMMON_H
9
#define COMMON_H
10

  
11
#define PROC_STAT "/proc/stat"
12

  
13
/** Write yes to stdout and return 0. The intended use is give an autoconf
14
 * response like "return writeyes();".
15
 * @returns a success state to be passed on as the return value from main */
16
int writeyes(void);
17

  
18
/** Answer an autoconf request by checking the readability of the given file.
19
 */
20
int autoconf_check_readable(const char *);
21

  
22
/** Obtain an integer value from the environment. In the absence of the
23
 * variable the given defaultvalue is returned.  */
24
int getenvint(const char *, int defaultvalue);
25

  
26
/** Print a name.warning line using the "name_warning" or "warning" environment
27
 * variables. */
28
void print_warning(const char *name);
29

  
30
/** Print a name.critical line using the "name_critical" or "critical"
31
 * environment variables. */
32
void print_critical(const char *name);
33

  
34
/** Print both name.warning and name.critical lines using environment
35
 * variables. */
36
void print_warncrit(const char *name);
37

  
38
/** Fail by printing the given message and a newline to stderr.
39
 * @returns a failure state to be passed on as the return value from main */
40
int fail(const char *message);
41

  
42
#define xisspace(x) isspace((int)(unsigned char) x)
43
#define xisdigit(x) isdigit((int)(unsigned char) x)
44

  
45
#endif
plugins/disk/smart-c/smart_.c
68 68
	return 0;
69 69
}
70 70

  
71
int smart(int argc, char **argv)
71
int main(int argc, char **argv)
72 72
{
73 73
	char command[50];
74 74
	char output[255];

Formats disponibles : Unified diff