Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / system / multicpu1sec / multicpu1sec-c.c @ 99d3a426

Historique | Voir | Annoter | Télécharger (3,39 ko)

1
/*
2
 * multicpu1sec C plugin
3
 */
4
#include <stdlib.h>
5
#include <stdio.h>
6
#include <string.h>
7
#include <unistd.h>
8

    
9
#include <time.h>
10

    
11
#include <sys/file.h>
12

    
13
#define PROC_STAT "/proc/stat"
14

    
15
int fail(char* msg) {
16
        perror(msg);
17

    
18
        return 1;
19
}
20

    
21
int config() {
22
        /* Get the number of CPU */
23
        FILE* f;
24
        if ( !(f=fopen(PROC_STAT, "r")) ) {
25
                return fail("cannot open " PROC_STAT);
26
        }
27

    
28
        // Starting with -1, since the first line is the "global cpu line"
29
        int ncpu = -1;
30
        while (! feof(f)) {
31
                char buffer[1024];
32
                if (fgets(buffer, 1024, f) == 0) {
33
                        break;
34
                }
35

    
36
                if (! strncmp(buffer, "cpu", 3)) ncpu ++;
37
        }
38

    
39
        fclose(f);
40

    
41
        printf(
42
                "graph_title multicpu1sec\n"
43
                "graph_category system::1sec\n"
44
                "graph_vlabel average cpu use %%\n"
45
                "graph_scale no\n"
46
                "graph_total All CPUs\n"
47
                "update_rate 1\n"
48
                "graph_data_size custom 1d, 10s for 1w, 1m for 1t, 5m for 1y\n"
49
        );
50

    
51
        int i;
52
        for (i = 0; i < ncpu; i++) {
53
                printf("cpu%d.label CPU %d\n", i, i);
54
                printf("cpu%d.draw %s\n", i, "AREASTACK");
55
                printf("cpu%d.type %s\n", i, "DERIVE");
56
                printf("cpu%d.min 0\n", i);
57
        }
58

    
59

    
60
        return 0;
61
}
62

    
63
char* pid_filename = "./multicpu1sec.pid";
64
char* cache_filename = "./multicpu1sec.value";
65

    
66
/* Wait until the next second, and return the EPOCH */
67
time_t wait_until_next_second() {
68
        struct timespec tp;
69
        clock_gettime(CLOCK_REALTIME, &tp);
70

    
71
        time_t current_epoch = tp.tv_sec;
72
        long nsec_to_sleep = 1000*1000*1000 - tp.tv_nsec;
73

    
74

    
75
        /* Only sleep if needed */
76
        if (nsec_to_sleep > 0) {
77
                tp.tv_sec = 0;
78
                tp.tv_nsec = nsec_to_sleep;
79
                nanosleep(&tp, NULL);
80
        }
81

    
82
        return current_epoch + 1;
83
}
84

    
85
int acquire() {
86

    
87
        /* fork ourselves if not asked otherwise */
88
        char* no_fork = getenv("no_fork");
89
        if (! no_fork || strcmp("1", no_fork)) {
90
                if (fork()) return;
91
                // we are the child, complete the daemonization
92

    
93
                /* Close standard IO */
94
                fclose(stdin);
95
                fclose(stdout);
96
                fclose(stderr);
97

    
98
                /* create new session and process group */
99
                setsid();
100
        }
101

    
102
        /* write the pid */
103
        FILE* pid_file = fopen(pid_filename, "w");
104
        fprintf(pid_file, "%d\n", getpid());
105
        fclose(pid_file);
106

    
107
        /* loop each second */
108
        while (1) {
109
                /* wait until next second */
110
                time_t epoch = wait_until_next_second();
111

    
112
                /* Reading /proc/stat */
113
                FILE* f = fopen(PROC_STAT, "r");
114
                // Read and ignore the 1rst line
115
                char buffer[1024];
116
                fgets(buffer, 1024, f);
117

    
118
                /* open the spoolfile */
119
                FILE* cache_file = fopen(cache_filename, "a");
120
                /* lock */
121
                flock(fileno(cache_file), LOCK_EX);
122

    
123
                while (! feof(f)) {
124
                        if (fgets(buffer, 1024, f) == 0) {
125
                                // EOF
126
                                break;
127
                        }
128

    
129
                        // Not on CPU lines anymore
130
                        if (strncmp(buffer, "cpu", 3)) break;
131

    
132
                        char cpu_id[64];
133
                        long usr, nice, sys, idle, iowait, irq, softirq;
134
                        sscanf(buffer, "%s %ld %ld %ld %ld %ld %ld %ld", cpu_id, &usr, &nice, &sys, &idle, &iowait, &irq, &softirq);
135

    
136
                        long used = usr + nice + sys + iowait + irq + softirq;
137

    
138
                        fprintf(cache_file, "%s.value %ld:%ld\n", cpu_id, epoch, used);
139
                }
140

    
141
                fclose(cache_file);
142
                fclose(f);
143
        }
144
}
145

    
146
int fetch() {
147
        FILE* cache_file = fopen(cache_filename, "r+");
148

    
149
        /* lock */
150
        flock(fileno(cache_file), LOCK_EX);
151

    
152
        /* cat the cache_file to stdout */
153
        char buffer[1024];
154
        while (fgets(buffer, 1024, cache_file)) {
155
                printf("%s", buffer);
156
        }
157

    
158
        ftruncate(fileno(cache_file), 0);
159
        fclose(cache_file);
160
}
161

    
162
int main(int argc, char **argv) {
163
        if (argc > 1) {
164
                char* first_arg = argv[1];
165
                if (! strcmp(first_arg, "config")) {
166
                        return config();
167
                }
168

    
169
                if (! strcmp(first_arg, "acquire")) {
170
                        return acquire();
171
                }
172
        }
173

    
174
        return fetch();
175
}