Projet

Général

Profil

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

root / plugins / system / multicpu1sec / multicpu1sec-c.c @ c86e4ab2

Historique | Voir | Annoter | Télécharger (2,93 ko)

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

    
8
#include <time.h>
9

    
10
#define PROC_STAT "/proc/stat"
11

    
12
int fail(char* msg) {
13
        perror(msg);
14

    
15
        return 1;
16
}
17

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

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

    
33
                if (! strncmp(buffer, "cpu", 3)) ncpu ++;
34
        }
35

    
36
        fclose(f);
37

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

    
48
        int i;
49
        for (i = 0; i < ncpu; i++) {
50
                printf("cpu%d.label CPU %d\n", i, i);
51
                printf("cpu%d.draw %s\n", i, "AREASTACK");
52
                printf("cpu%d.min 0\n", i);
53
        }
54

    
55

    
56
        return 0;
57
}
58

    
59
char* pid_filename = "./multicpu1sec.pid";
60
char* cache_filename = "./multicpu1sec.value";
61

    
62
/* Wait until the next second, and return the EPOCH */
63
time_t wait_until_next_second() {
64
        struct timespec tp;
65
        clock_gettime(CLOCK_REALTIME, &tp);
66

    
67
        time_t current_epoch = tp.tv_sec;
68
        long nsec_to_sleep = 1000*1000*1000 - tp.tv_nsec;
69

    
70

    
71
        /* Only sleep if needed */
72
        if (nsec_to_sleep > 0) {
73
                tp.tv_sec = 0;
74
                tp.tv_nsec = nsec_to_sleep;
75
                nanosleep(&tp, NULL);
76
        }
77

    
78
        return current_epoch + 1;
79
}
80

    
81
int acquire() {
82
        printf("acquire()\n");
83

    
84
        /* write the pid */
85
        FILE* pid_file = fopen(pid_filename, "w");
86
        fprintf(pid_file, "%d\n", getpid());
87
        fclose(pid_file);
88

    
89
        /* loop each second */
90
        while (1) {
91
                /* wait until next second */
92
                time_t epoch = wait_until_next_second();
93

    
94
                /* Reading /proc/stat */
95
                FILE* f = fopen(PROC_STAT, "r");
96
                // Read and ignore the 1rst line
97
                char buffer[1024];
98
                fgets(buffer, 1024, f);
99

    
100
                /* open the spoolfile */
101

    
102
                FILE* cache_file = fopen(cache_filename, "a");
103

    
104
                while (! feof(f)) {
105
                        if (fgets(buffer, 1024, f) == 0) {
106
                                // EOF
107
                                break;
108
                        }
109

    
110
                        // Not on CPU lines anymore
111
                        if (strncmp(buffer, "cpu", 3)) break;
112

    
113
                        char cpu_id[64];
114
                        long usr, nice, sys, idle, iowait, irq, softirq;
115
                        sscanf(buffer, "%s %ld %ld %ld %ld %ld", cpu_id, &usr, &nice, &sys, &idle, &iowait, &irq, &softirq);
116

    
117
                        long used = usr + nice + sys + idle + iowait + irq + softirq;
118

    
119
                        fprintf(cache_file, "%s.value %ld:%ld\n", cpu_id, epoch, used);
120
                }
121

    
122
                fclose(cache_file);
123
                fclose(f);
124
        }
125
}
126

    
127
int fetch() {
128
        printf("fetch()\n");
129
        FILE* cache_file = fopen(cache_filename, "r");
130

    
131
        /* cat the cache_file to stdout */
132
        char buffer[1024];
133
        while (fgets(buffer, 1024, cache_file)) {
134
                printf("%s", buffer);
135
        }
136

    
137
        ftruncate(fileno(cache_file), 0);
138
        fclose(cache_file);
139
}
140

    
141
int main(int argc, char **argv) {
142
        if (argc > 1) {
143
                char* first_arg = argv[1];
144
                if (! strcmp(first_arg, "config")) {
145
                        return config();
146
                }
147

    
148
                if (! strcmp(first_arg, "acquire")) {
149
                        return acquire();
150
                }
151
        }
152

    
153
        return fetch();
154
}