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