]> arthur.barton.de Git - netdata.git/blob - src/plugin_proc.c
Merge pull request #1998 from ktsaou/master
[netdata.git] / src / plugin_proc.c
1 #include "common.h"
2
3 static struct proc_module {
4     const char *name;
5     const char *dim;
6
7     int enabled;
8
9     int (*func)(int update_every, usec_t dt);
10     usec_t duration;
11
12     RRDDIM *rd;
13
14 } proc_modules[] = {
15
16         // system metrics
17         { .name = "/proc/stat", .dim = "stat", .func = do_proc_stat },
18         { .name = "/proc/uptime", .dim = "uptime", .func = do_proc_uptime },
19         { .name = "/proc/loadavg", .dim = "loadavg", .func = do_proc_loadavg },
20         { .name = "/proc/sys/kernel/random/entropy_avail", .dim = "entropy", .func = do_proc_sys_kernel_random_entropy_avail },
21
22         // CPU metrics
23         { .name = "/proc/interrupts", .dim = "interrupts", .func = do_proc_interrupts },
24         { .name = "/proc/softirqs", .dim = "softirqs", .func = do_proc_softirqs },
25
26         // memory metrics
27         { .name = "/proc/vmstat", .dim = "vmstat", .func = do_proc_vmstat },
28         { .name = "/proc/meminfo", .dim = "meminfo", .func = do_proc_meminfo },
29         { .name = "/sys/kernel/mm/ksm", .dim = "ksm", .func = do_sys_kernel_mm_ksm },
30         { .name = "/sys/devices/system/edac/mc", .dim = "ecc", .func = do_proc_sys_devices_system_edac_mc },
31         { .name = "/sys/devices/system/node", .dim = "numa", .func = do_proc_sys_devices_system_node },
32
33         // network metrics
34         { .name = "/proc/net/dev", .dim = "netdev", .func = do_proc_net_dev },
35         { .name = "/proc/net/netstat", .dim = "netstat", .func = do_proc_net_netstat },
36         { .name = "/proc/net/snmp", .dim = "snmp", .func = do_proc_net_snmp },
37         { .name = "/proc/net/snmp6", .dim = "snmp6", .func = do_proc_net_snmp6 },
38         { .name = "/proc/net/softnet_stat", .dim = "softnet", .func = do_proc_net_softnet_stat },
39         { .name = "/proc/net/ip_vs/stats", .dim = "ipvs", .func = do_proc_net_ip_vs_stats },
40
41         // firewall metrics
42         { .name = "/proc/net/stat/conntrack", .dim = "conntrack", .func = do_proc_net_stat_conntrack },
43         { .name = "/proc/net/stat/synproxy", .dim = "synproxy", .func = do_proc_net_stat_synproxy },
44
45         // disk metrics
46         { .name = "/proc/diskstats", .dim = "diskstats", .func = do_proc_diskstats },
47
48         // NFS metrics
49         { .name = "/proc/net/rpc/nfsd", .dim = "nfsd", .func = do_proc_net_rpc_nfsd },
50         { .name = "/proc/net/rpc/nfs", .dim = "nfs", .func = do_proc_net_rpc_nfs },
51
52         // IPC metrics
53         { .name = "ipc", .dim = "ipc", .func = do_ipc },
54
55         // the terminator of this array
56         { .name = NULL, .dim = NULL, .func = NULL }
57 };
58
59 void *proc_main(void *ptr) {
60     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
61
62     info("PROC Plugin thread created with task id %d", gettid());
63
64     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
65         error("Cannot set pthread cancel type to DEFERRED.");
66
67     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
68         error("Cannot set pthread cancel state to ENABLE.");
69
70     int vdo_cpu_netdata = config_get_boolean("plugin:proc", "netdata server resources", 1);
71
72     // check the enabled status for each module
73     int i;
74     for(i = 0 ; proc_modules[i].name ;i++) {
75         struct proc_module *pm = &proc_modules[i];
76
77         pm->enabled = config_get_boolean("plugin:proc", pm->name, 1);
78         pm->duration = 0ULL;
79         pm->rd = NULL;
80     }
81
82     usec_t step = localhost->rrd_update_every * USEC_PER_SEC;
83     heartbeat_t hb;
84     heartbeat_init(&hb);
85
86     for(;;) {
87         usec_t hb_dt = heartbeat_next(&hb, step);
88         usec_t duration = 0ULL;
89
90         if(unlikely(netdata_exit)) break;
91
92         // BEGIN -- the job to be done
93
94         for(i = 0 ; proc_modules[i].name ;i++) {
95             struct proc_module *pm = &proc_modules[i];
96             if(unlikely(!pm->enabled)) continue;
97
98             debug(D_PROCNETDEV_LOOP, "PROC calling %s.", pm->name);
99
100             pm->enabled = !pm->func(localhost->rrd_update_every, hb_dt);
101             pm->duration = heartbeat_dt_usec(&hb) - duration;
102             duration += pm->duration;
103
104             if(unlikely(netdata_exit)) break;
105         }
106
107         // END -- the job is done
108
109         // --------------------------------------------------------------------
110
111         if(vdo_cpu_netdata) {
112             static RRDSET *st = NULL;
113
114             if(unlikely(!st)) {
115                 st = rrdset_find_bytype_localhost("netdata", "plugin_proc_modules");
116
117                 if(!st) {
118                     st = rrdset_create_localhost("netdata", "plugin_proc_modules", NULL, "proc", NULL
119                                                  , "NetData Proc Plugin Modules Durations", "milliseconds/run", 132001
120                                                  , localhost->rrd_update_every, RRDSET_TYPE_STACKED);
121
122                     for(i = 0 ; proc_modules[i].name ;i++) {
123                         struct proc_module *pm = &proc_modules[i];
124                         if(unlikely(!pm->enabled)) continue;
125
126                         pm->rd = rrddim_add(st, pm->dim, NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
127                     }
128                 }
129             }
130             else rrdset_next(st);
131
132             for(i = 0 ; proc_modules[i].name ;i++) {
133                 struct proc_module *pm = &proc_modules[i];
134                 if(unlikely(!pm->enabled)) continue;
135
136                 rrddim_set_by_pointer(st, pm->rd, pm->duration);
137             }
138             rrdset_done(st);
139
140             global_statistics_charts();
141             registry_statistics();
142         }
143     }
144
145     info("PROC thread exiting");
146
147     static_thread->enabled = 0;
148     pthread_exit(NULL);
149     return NULL;
150 }
151
152 int get_numa_node_count(void)
153 {
154     static int numa_node_count = -1;
155
156     if (numa_node_count != -1)
157         return numa_node_count;
158
159     numa_node_count = 0;
160
161     char name[FILENAME_MAX + 1];
162     snprintfz(name, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/devices/system/node");
163     char *dirname = config_get("plugin:proc:/sys/devices/system/node", "directory to monitor", name);
164
165     DIR *dir = opendir(dirname);
166     if(dir) {
167         struct dirent *de = NULL;
168         while((de = readdir(dir))) {
169             if(de->d_type != DT_DIR)
170                 continue;
171
172             if(strncmp(de->d_name, "node", 4) != 0)
173                 continue;
174
175             if(!isdigit(de->d_name[4]))
176                 continue;
177
178             numa_node_count++;
179         }
180         closedir(dir);
181     }
182
183     return numa_node_count;
184 }