]> arthur.barton.de Git - netdata.git/blob - src/plugin_proc.c
dns_query_time plugin: replace "." with "_" in dimensions
[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         // ZFS metrics
53         { .name = "/proc/spl/kstat/zfs/arcstats", .dim = "zfs_arcstats", .func = do_proc_spl_kstat_zfs_arcstats },
54
55         // IPC metrics
56         { .name = "ipc", .dim = "ipc", .func = do_ipc },
57
58         // the terminator of this array
59         { .name = NULL, .dim = NULL, .func = NULL }
60 };
61
62 void *proc_main(void *ptr) {
63     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
64
65     info("PROC Plugin thread created with task id %d", gettid());
66
67     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
68         error("Cannot set pthread cancel type to DEFERRED.");
69
70     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
71         error("Cannot set pthread cancel state to ENABLE.");
72
73     int vdo_cpu_netdata = config_get_boolean("plugin:proc", "netdata server resources", 1);
74
75     // check the enabled status for each module
76     int i;
77     for(i = 0 ; proc_modules[i].name ;i++) {
78         struct proc_module *pm = &proc_modules[i];
79
80         pm->enabled = config_get_boolean("plugin:proc", pm->name, 1);
81         pm->duration = 0ULL;
82         pm->rd = NULL;
83     }
84
85     usec_t step = localhost->rrd_update_every * USEC_PER_SEC;
86     heartbeat_t hb;
87     heartbeat_init(&hb);
88
89     for(;;) {
90         usec_t hb_dt = heartbeat_next(&hb, step);
91         usec_t duration = 0ULL;
92
93         if(unlikely(netdata_exit)) break;
94
95         // BEGIN -- the job to be done
96
97         for(i = 0 ; proc_modules[i].name ;i++) {
98             struct proc_module *pm = &proc_modules[i];
99             if(unlikely(!pm->enabled)) continue;
100
101             debug(D_PROCNETDEV_LOOP, "PROC calling %s.", pm->name);
102
103             pm->enabled = !pm->func(localhost->rrd_update_every, hb_dt);
104             pm->duration = heartbeat_dt_usec(&hb) - duration;
105             duration += pm->duration;
106
107             if(unlikely(netdata_exit)) break;
108         }
109
110         // END -- the job is done
111
112         // --------------------------------------------------------------------
113
114         if(vdo_cpu_netdata) {
115             static RRDSET *st = NULL;
116
117             if(unlikely(!st)) {
118                 st = rrdset_find_bytype_localhost("netdata", "plugin_proc_modules");
119
120                 if(!st) {
121                     st = rrdset_create_localhost("netdata", "plugin_proc_modules", NULL, "proc", NULL
122                                                  , "NetData Proc Plugin Modules Durations", "milliseconds/run", 132001
123                                                  , localhost->rrd_update_every, RRDSET_TYPE_STACKED);
124
125                     for(i = 0 ; proc_modules[i].name ;i++) {
126                         struct proc_module *pm = &proc_modules[i];
127                         if(unlikely(!pm->enabled)) continue;
128
129                         pm->rd = rrddim_add(st, pm->dim, NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
130                     }
131                 }
132             }
133             else rrdset_next(st);
134
135             for(i = 0 ; proc_modules[i].name ;i++) {
136                 struct proc_module *pm = &proc_modules[i];
137                 if(unlikely(!pm->enabled)) continue;
138
139                 rrddim_set_by_pointer(st, pm->rd, pm->duration);
140             }
141             rrdset_done(st);
142
143             global_statistics_charts();
144             registry_statistics();
145         }
146     }
147
148     info("PROC thread exiting");
149
150     static_thread->enabled = 0;
151     pthread_exit(NULL);
152     return NULL;
153 }
154
155 int get_numa_node_count(void)
156 {
157     static int numa_node_count = -1;
158
159     if (numa_node_count != -1)
160         return numa_node_count;
161
162     numa_node_count = 0;
163
164     char name[FILENAME_MAX + 1];
165     snprintfz(name, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/devices/system/node");
166     char *dirname = config_get("plugin:proc:/sys/devices/system/node", "directory to monitor", name);
167
168     DIR *dir = opendir(dirname);
169     if(dir) {
170         struct dirent *de = NULL;
171         while((de = readdir(dir))) {
172             if(de->d_type != DT_DIR)
173                 continue;
174
175             if(strncmp(de->d_name, "node", 4) != 0)
176                 continue;
177
178             if(!isdigit(de->d_name[4]))
179                 continue;
180
181             numa_node_count++;
182         }
183         closedir(dir);
184     }
185
186     return numa_node_count;
187 }