10 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <sys/select.h>
16 #include <sys/resource.h>
18 #include <sys/sendfile.h>
20 #include <arpa/inet.h>
21 #include <netinet/in.h>
22 #include <netinet/tcp.h>
40 // enabling this will detach the plugins from netdata
41 // each plugin will have its own process group
42 // #define DETACH_PLUGINS_FROM_NETDATA
44 #define RRD_TYPE_NET "net"
45 #define RRD_TYPE_NET_LEN strlen(RRD_TYPE_NET)
47 #define RRD_TYPE_TC "tc"
48 #define RRD_TYPE_TC_LEN strlen(RRD_TYPE_TC)
50 #define RRD_TYPE_DISK "disk"
51 #define RRD_TYPE_DISK_LEN strlen(RRD_TYPE_DISK)
53 #define RRD_TYPE_NET_SNMP "ipv4"
54 #define RRD_TYPE_NET_SNMP_LEN strlen(RRD_TYPE_NET_SNMP)
56 #define RRD_TYPE_NET_STAT_CONNTRACK "conntrack"
57 #define RRD_TYPE_NET_STAT_CONNTRACK_LEN strlen(RRD_TYPE_NET_STAT_CONNTRACK)
59 #define RRD_TYPE_NET_IPVS "ipvs"
60 #define RRD_TYPE_NET_IPVS_LEN strlen(RRD_TYPE_NET_IPVS)
62 #define RRD_TYPE_STAT "cpu"
63 #define RRD_TYPE_STAT_LEN strlen(RRD_TYPE_STAT)
65 #define WEB_PATH_FILE "file"
66 #define WEB_PATH_DATA "data"
67 #define WEB_PATH_DATASOURCE "datasource"
68 #define WEB_PATH_GRAPH "graph"
70 // type of JSON generations
71 #define DATASOURCE_JSON 0
72 #define DATASOURCE_GOOGLE_JSON 1
73 #define DATASOURCE_GOOGLE_JSONP 2
76 #define UPDATE_EVERY 1
77 #define UPDATE_EVERY_MAX 3600
78 #define LISTEN_PORT 19999
80 #define HISTORY_MAX (86400*10)
81 #define SAVE_PATH "/tmp"
83 #define D_WEB_BUFFER 0x00000001
84 #define D_WEB_CLIENT 0x00000002
85 #define D_LISTENER 0x00000004
86 #define D_WEB_DATA 0x00000008
87 #define D_OPTIONS 0x00000010
88 #define D_PROCNETDEV_LOOP 0x00000020
89 #define D_RRD_STATS 0x00000040
90 #define D_WEB_CLIENT_ACCESS 0x00000080
91 #define D_TC_LOOP 0x00000100
92 #define D_DEFLATE 0x00000200
93 #define D_CONFIG 0x00000400
94 #define D_PLUGINSD 0x00000800
95 #define D_CHILDS 0x00001000
96 #define D_EXIT 0x00002000
98 #define CT_APPLICATION_JSON 1
99 #define CT_TEXT_PLAIN 2
100 #define CT_TEXT_HTML 3
101 #define CT_APPLICATION_X_JAVASCRIPT 4
102 #define CT_TEXT_CSS 5
103 #define CT_TEXT_XML 6
104 #define CT_APPLICATION_XML 7
105 #define CT_TEXT_XSL 8
106 #define CT_APPLICATION_OCTET_STREAM 9
107 #define CT_APPLICATION_X_FONT_TRUETYPE 10
108 #define CT_APPLICATION_X_FONT_OPENTYPE 11
109 #define CT_APPLICATION_FONT_WOFF 12
110 #define CT_APPLICATION_VND_MS_FONTOBJ 13
111 #define CT_IMAGE_SVG_XML 14
114 #define DEBUG (D_WEB_CLIENT_ACCESS|D_LISTENER|D_RRD_STATS)
115 //#define DEBUG 0xffffffff
118 #define HOSTNAME_MAX 1024
121 #define EXIT_FAILURE 1
122 #define LISTEN_BACKLOG 100
124 #define INITIAL_WEB_DATA_LENGTH 65536
125 #define WEB_DATA_LENGTH_INCREASE_STEP 65536
126 #define ZLIB_CHUNK 16384
128 #define MAX_HTTP_HEADER_SIZE 16384
130 #define MAX_PROC_NET_SNMP_LINE 4096
131 #define MAX_PROC_NET_SNMP_NAME 1024
133 #define MAX_PROC_NET_STAT_CONNTRACK_LINE 4096
134 #define MAX_PROC_NET_STAT_CONNTRACK_NAME 1024
136 #define MAX_PROC_NET_IPVS_LINE 4096
137 #define MAX_PROC_NET_IPVS_NAME 1024
139 #define MAX_PROC_STAT_LINE 4096
140 #define MAX_PROC_STAT_NAME 1024
144 int save_history = HISTORY;
145 int update_every = UPDATE_EVERY;
146 int listen_port = LISTEN_PORT;
149 // ----------------------------------------------------------------------------
152 struct global_statistics {
154 unsigned long long connected_clients;
156 unsigned long long web_requests;
158 unsigned long long bytes_received;
159 unsigned long long bytes_sent;
161 } global_statistics = { 0ULL, 0ULL, 0ULL, 0ULL };
163 pthread_mutex_t global_statistics_mutex = PTHREAD_MUTEX_INITIALIZER;
165 void global_statistics_lock(void)
167 pthread_mutex_lock(&global_statistics_mutex);
169 void global_statistics_unlock(void)
171 pthread_mutex_unlock(&global_statistics_mutex);
174 // ----------------------------------------------------------------------------
177 unsigned long long debug_flags = DEBUG;
180 FILE *stdaccess = NULL;
182 int access_log_syslog = 1;
183 int error_log_syslog = 1;
184 int output_log_syslog = 1; // debug log
187 void log_date(FILE *out)
196 if (tmp == NULL) return;
197 if (strftime(outstr, sizeof(outstr), "%y-%m-%d %H:%M:%S", tmp) == 0) return;
199 fprintf(out, "%s: ", outstr);
203 //#define debug(args...) debug_int(__FILE__, __FUNCTION__, __LINE__, ##args)
204 #define debug(type, args...) do { if(!silent && debug_flags & type) debug_int(__FILE__, __FUNCTION__, __LINE__, ##args); } while(0)
206 void debug_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
212 va_start( args, fmt );
213 fprintf(stdout, "DEBUG (%04lu@%-15.15s): ", line, function);
214 vfprintf( stdout, fmt, args );
216 fprintf(stdout, "\n");
218 if(output_log_syslog) {
219 va_start( args, fmt );
220 vsyslog(LOG_ERR, fmt, args );
225 #define info(args...) info_int(__FILE__, __FUNCTION__, __LINE__, ##args)
227 void info_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
234 va_start( args, fmt );
235 if(debug_flags) fprintf(stderr, "INFO (%04lu@%-15.15s): ", line, function);
236 else fprintf(stderr, "INFO: ");
237 vfprintf( stderr, fmt, args );
240 fprintf(stderr, "\n");
242 if(error_log_syslog) {
243 va_start( args, fmt );
244 vsyslog(LOG_INFO, fmt, args );
249 #define error(args...) error_int(__FILE__, __FUNCTION__, __LINE__, ##args)
251 void error_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
258 va_start( args, fmt );
259 if(debug_flags) fprintf(stderr, "ERROR (%04lu@%-15.15s): ", line, function);
260 else fprintf(stderr, "ERROR: ");
261 vfprintf( stderr, fmt, args );
265 fprintf(stderr, " (errno %d, %s)\n", errno, strerror(errno));
268 else fprintf(stderr, "\n");
270 if(error_log_syslog) {
271 va_start( args, fmt );
272 vsyslog(LOG_ERR, fmt, args );
277 #define fatal(args...) fatal_int(__FILE__, __FUNCTION__, __LINE__, ##args)
279 void fatal_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
286 va_start( args, fmt );
287 if(debug_flags) fprintf(stderr, "FATAL (%04lu@%-15.15s): ", line, function);
288 else fprintf(stderr, "FATAL: ");
289 vfprintf( stderr, fmt, args );
293 fprintf(stderr, "\n");
295 if(error_log_syslog) {
296 va_start( args, fmt );
297 vsyslog(LOG_CRIT, fmt, args );
304 void log_access( const char *fmt, ... )
311 va_start( args, fmt );
312 vfprintf( stdaccess, fmt, args );
314 fprintf( stdaccess, "\n");
318 if(access_log_syslog) {
319 va_start( args, fmt );
320 vsyslog(LOG_INFO, fmt, args );
326 // ----------------------------------------------------------------------------
329 unsigned long long usecdiff(struct timeval *now, struct timeval *last) {
330 return ((((now->tv_sec * 1000000ULL) + now->tv_usec) - ((last->tv_sec * 1000000ULL) + last->tv_usec)));
333 unsigned long simple_hash(const char *name)
335 int i, len = strlen(name);
336 unsigned long hash = 0;
338 for(i = 0; i < len ;i++) hash += (i * name[i]) + i + name[i];
346 FILE *mypopen(const char *command, pid_t *pidptr)
350 if(pipe(pipefd) == -1) return NULL;
354 close(pipefd[PIPE_READ]);
355 close(pipefd[PIPE_WRITE]);
361 close(pipefd[PIPE_WRITE]);
362 FILE *fp = fdopen(pipefd[PIPE_READ], "r");
369 for(i = sysconf(_SC_OPEN_MAX); i > 0; i--)
370 if(i != STDIN_FILENO && i != STDERR_FILENO && i != pipefd[PIPE_WRITE]) close(i);
372 // move the pipe to stdout
373 if(pipefd[PIPE_WRITE] != STDOUT_FILENO) {
374 dup2(pipefd[PIPE_WRITE], STDOUT_FILENO);
375 close(pipefd[PIPE_WRITE]);
378 #ifdef DETACH_PLUGINS_FROM_NETDATA
379 // this was an attempt to detach the child and use the suspend mode charts.d
380 // unfortunatelly it does not work as expected.
382 // fork again to become session leader
384 if(pid == -1) fprintf(stderr, "Cannot fork again on pid %d\n", getpid());
390 // set a new process group id for just this child
391 if( setpgid(0, 0) != 0 )
392 fprintf(stderr, "Cannot set a new process group for pid %d (%s)\n", getpid(), strerror(errno));
394 if( getpgid(0) != getpid() )
395 fprintf(stderr, "Process group set is incorrect. Expected %d, found %d\n", getpid(), getpgid(0));
398 fprintf(stderr, "Cannot set session id for pid %d (%s)\n", getpid(), strerror(errno));
400 fprintf(stdout, "MYPID %d\n", getpid());
404 // ignore all signals
405 for (i = 1 ; i < 65 ;i++) if(i != SIGSEGV) signal(i, SIG_DFL);
407 fprintf(stderr, "executing command: '%s' on pid %d.\n", command, getpid());
408 execl("/bin/sh", "sh", "-c", command, NULL);
412 void mypclose(FILE *fp)
414 // this is a very poor implementation of pclose()
415 // the caller should catch SIGCHLD and waitpid() on the exited child
416 // otherwise the child will be a zombie forever
422 // ----------------------------------------------------------------------------
423 // URL encode / decode
424 // code from: http://www.geekhideout.com/urlcode.shtml
426 /* Converts a hex character to its integer value */
427 char from_hex(char ch) {
428 return (char)(isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10);
431 /* Converts an integer value to its hex character*/
432 char to_hex(char code) {
433 static char hex[] = "0123456789abcdef";
434 return hex[code & 15];
437 /* Returns a url-encoded version of str */
438 /* IMPORTANT: be sure to free() the returned string after use */
439 char *url_encode(char *str) {
441 *buf = malloc(strlen(str) * 3 + 1),
445 if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
448 else if (*pstr == ' ')
452 *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
462 /* Returns a url-decoded version of str */
463 /* IMPORTANT: be sure to free() the returned string after use */
464 char *url_decode(char *str) {
466 *buf = malloc(strlen(str) + 1),
469 if(!buf) fatal("Cannot allocate memory.");
473 if (pstr[1] && pstr[2]) {
474 *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
478 else if (*pstr == '+')
493 // ----------------------------------------------------------------------------
498 int create_listen_socket(int port)
502 struct sockaddr_in name;
504 debug(D_LISTENER, "Creating new listening socket on port %d", port);
506 sock = socket(AF_INET, SOCK_STREAM, 0);
508 fatal("socket() failed, errno=%d", errno);
510 /* avoid "address already in use" */
511 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt));
513 memset(&name, 0, sizeof(struct sockaddr_in));
514 name.sin_family = AF_INET;
515 name.sin_port = htons (port);
516 name.sin_addr.s_addr = htonl (INADDR_ANY);
517 if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0)
518 fatal("bind() failed, errno=%d", errno);
520 if (listen(sock, LISTEN_BACKLOG) < 0)
521 fatal("listen() failed, errno=%d", errno);
523 debug(D_LISTENER, "Listening Port %d created", port);
528 // ----------------------------------------------------------------------------
531 #define CONFIG_MAX_NAME 100
532 #define CONFIG_MAX_VALUE 1024
533 #define CONFIG_FILENAME "netdata.conf"
534 #define CONFIG_FILE_LINE_MAX 4096
536 pthread_rwlock_t config_rwlock = PTHREAD_RWLOCK_INITIALIZER;
538 struct config_value {
539 char name[CONFIG_MAX_NAME + 1];
540 char value[CONFIG_MAX_VALUE + 1];
542 unsigned long hash; // a simple hash to speed up searching
543 // we first compare hashes, and only if the hashes are equal we do string comparisons
545 int loaded; // loaded from the user config
546 int used; // has been accessed from the program
547 int changed; // changed from the internal default
549 struct config_value *next;
553 char name[CONFIG_MAX_NAME + 1];
555 unsigned long hash; // a simple hash to speed up searching
556 // we first compare hashes, and only if the hashes are equal we do string comparisons
558 struct config_value *values;
561 } *config_root = NULL;
563 struct config_value *config_value_create(struct config *co, const char *name, const char *value)
565 debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
567 struct config_value *cv = calloc(1, sizeof(struct config_value));
568 if(!cv) fatal("Cannot allocate config_value");
570 strncpy(cv->name, name, CONFIG_MAX_NAME);
571 strncpy(cv->value, value, CONFIG_MAX_VALUE);
572 cv->hash = simple_hash(cv->name);
574 // no need for string termination, due to calloc()
576 struct config_value *cv2 = co->values;
578 while (cv2->next) cv2 = cv2->next;
581 else co->values = cv;
586 struct config *config_create(const char *section)
588 debug(D_CONFIG, "Creating section '%s'.", section);
590 struct config *co = calloc(1, sizeof(struct config));
591 if(!co) fatal("Cannot allocate config");
593 strncpy(co->name, section, CONFIG_MAX_NAME);
594 co->hash = simple_hash(co->name);
596 // no need for string termination, due to calloc()
598 struct config *co2 = config_root;
600 while (co2->next) co2 = co2->next;
603 else config_root = co;
608 struct config *config_find_section(const char *section)
611 unsigned long hash = simple_hash(section);
613 for(co = config_root; co ; co = co->next)
615 if(strcmp(co->name, section) == 0)
623 // skip leading spaces
624 while(*s && isspace(*s)) s++;
625 if(!*s || *s == '#') return NULL;
627 // skip tailing spaces
628 int c = strlen(s) - 1;
629 while(c >= 0 && isspace(s[c])) {
633 if(c < 0) return NULL;
638 int load_config(char *filename, int overwrite_used)
641 struct config *co = NULL;
643 pthread_rwlock_wrlock(&config_rwlock);
645 char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
647 if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
648 FILE *fp = fopen(filename, "r");
650 error("Cannot open file '%s'", CONFIG_DIR "/" CONFIG_FILENAME);
651 pthread_rwlock_unlock(&config_rwlock);
655 while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
656 buffer[CONFIG_FILE_LINE_MAX] = '\0';
661 debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
666 if(*s == '[' && s[len - 1] == ']') {
671 co = config_find_section(s);
672 if(!co) co = config_create(s);
678 // line outside a section
679 error("Ignoring line %d ('%s'), it is outsize all sections.", line, s);
684 char *value = strchr(s, '=');
686 error("Ignoring line %d ('%s'), there is no = in it.", line, s);
696 error("Ignoring line %d, name is empty.", line);
700 debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
704 struct config_value *cv;
705 for(cv = co->values; cv ; cv = cv->next)
706 if(strcmp(cv->name, name) == 0) break;
708 if(!cv) cv = config_value_create(co, name, value);
710 if((cv->used && overwrite_used) || !cv->used) {
711 debug(D_CONFIG, "Overwriting '%s/%s'.", line, co->name, cv->name);
712 strncpy(cv->value, value, CONFIG_MAX_VALUE);
713 // termination is already there
716 debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
723 pthread_rwlock_unlock(&config_rwlock);
727 char *config_get(const char *section, const char *name, const char *default_value)
729 struct config_value *cv;
731 debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
733 pthread_rwlock_rdlock(&config_rwlock);
735 struct config *co = config_find_section(section);
736 if(!co) co = config_create(section);
738 unsigned long hash = simple_hash(name);
739 for(cv = co->values; cv ; cv = cv->next)
741 if(strcmp(cv->name, name) == 0)
744 if(!cv) cv = config_value_create(co, name, default_value);
747 if(cv->loaded || cv->changed) {
748 // this is a loaded value from the config file
749 // if it is different that the default, mark it
750 if(strcmp(cv->value, default_value) != 0) cv->changed = 1;
753 // this is not loaded from the config
754 // copy the default value to it
755 strncpy(cv->value, default_value, CONFIG_MAX_VALUE);
758 pthread_rwlock_unlock(&config_rwlock);
762 long long config_get_number(const char *section, const char *name, long long value)
764 char buffer[100], *s;
765 sprintf(buffer, "%lld", value);
767 s = config_get(section, name, buffer);
768 return strtoll(s, NULL, 0);
771 int config_get_boolean(const char *section, const char *name, int value)
777 s = config_get(section, name, s);
779 if(strcmp(s, "yes") == 0 || strcmp(s, "true") == 0 || strcmp(s, "1") == 0) {
789 const char *config_set(const char *section, const char *name, const char *value)
791 struct config_value *cv;
793 debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
795 pthread_rwlock_wrlock(&config_rwlock);
797 struct config *co = config_find_section(section);
798 if(!co) co = config_create(section);
800 unsigned long hash = simple_hash(name);
801 for(cv = co->values; cv ; cv = cv->next)
803 if(strcmp(cv->name, name) == 0)
806 if(!cv) cv = config_value_create(co, name, value);
809 if(strcmp(cv->value, value) != 0) cv->changed = 1;
811 strncpy(cv->value, value, CONFIG_MAX_VALUE);
812 // termination is already there
814 pthread_rwlock_unlock(&config_rwlock);
819 long long config_set_number(const char *section, const char *name, long long value)
822 sprintf(buffer, "%lld", value);
824 config_set(section, name, buffer);
829 int config_set_boolean(const char *section, const char *name, int value)
835 config_set(section, name, s);
841 // ----------------------------------------------------------------------------
844 #define CHART_TYPE_LINE_NAME "line"
845 #define CHART_TYPE_AREA_NAME "area"
846 #define CHART_TYPE_STACKED_NAME "stacked"
849 #define CHART_TYPE_LINE 0
850 #define CHART_TYPE_AREA 1
851 #define CHART_TYPE_STACKED 2
853 int chart_type_id(const char *name)
855 if(strcmp(name, CHART_TYPE_AREA_NAME) == 0) return CHART_TYPE_AREA;
856 if(strcmp(name, CHART_TYPE_STACKED_NAME) == 0) return CHART_TYPE_STACKED;
857 if(strcmp(name, CHART_TYPE_LINE_NAME) == 0) return CHART_TYPE_LINE;
858 return CHART_TYPE_LINE;
861 const char *chart_type_name(int chart_type)
863 static char line[] = CHART_TYPE_LINE_NAME;
864 static char area[] = CHART_TYPE_AREA_NAME;
865 static char stacked[] = CHART_TYPE_STACKED_NAME;
868 case CHART_TYPE_LINE:
871 case CHART_TYPE_AREA:
874 case CHART_TYPE_STACKED:
881 // ----------------------------------------------------------------------------
884 #define RRD_DIMENSION_ABSOLUTE_NAME "absolute"
885 #define RRD_DIMENSION_INCREMENTAL_NAME "incremental"
886 #define RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL_NAME "percentage-of-incremental-row"
887 #define RRD_DIMENSION_PCENT_OVER_ROW_TOTAL_NAME "percentage-of-absolute-row"
888 #define RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION_NAME "incremental-no-interpolation"
889 #define RRD_DIMENSION_ABSOLUTE_NO_INTERPOLATION_NAME "absolute-no-interpolation"
892 #define RRD_DIMENSION_ABSOLUTE 0
893 #define RRD_DIMENSION_INCREMENTAL 1
894 #define RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL 2
895 #define RRD_DIMENSION_PCENT_OVER_ROW_TOTAL 3
896 #define RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION 4
897 #define RRD_DIMENSION_ABSOLUTE_NO_INTERPOLATION 5
899 int algorithm_id(const char *name)
901 if(strcmp(name, RRD_DIMENSION_ABSOLUTE_NAME) == 0) return RRD_DIMENSION_ABSOLUTE;
902 if(strcmp(name, RRD_DIMENSION_ABSOLUTE_NO_INTERPOLATION_NAME) == 0) return RRD_DIMENSION_ABSOLUTE_NO_INTERPOLATION;
903 if(strcmp(name, RRD_DIMENSION_INCREMENTAL_NAME) == 0) return RRD_DIMENSION_INCREMENTAL;
904 if(strcmp(name, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION_NAME) == 0) return RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION;
905 if(strcmp(name, RRD_DIMENSION_PCENT_OVER_ROW_TOTAL_NAME) == 0) return RRD_DIMENSION_PCENT_OVER_ROW_TOTAL;
906 if(strcmp(name, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL_NAME) == 0) return RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL;
907 return RRD_DIMENSION_ABSOLUTE;
910 const char *algorithm_name(int chart_type)
912 static char absolute[] = RRD_DIMENSION_ABSOLUTE_NAME;
913 static char incremental[] = RRD_DIMENSION_INCREMENTAL_NAME;
914 static char percentage_of_absolute_row[] = RRD_DIMENSION_PCENT_OVER_ROW_TOTAL_NAME;
915 static char percentage_of_incremental_row[] = RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL_NAME;
916 static char incremental_last_value[] = RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION_NAME;
917 static char absolute_last_value[] = RRD_DIMENSION_ABSOLUTE_NO_INTERPOLATION_NAME;
920 case RRD_DIMENSION_ABSOLUTE:
923 case RRD_DIMENSION_ABSOLUTE_NO_INTERPOLATION:
924 return absolute_last_value;
926 case RRD_DIMENSION_INCREMENTAL:
929 case RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION:
930 return incremental_last_value;
932 case RRD_DIMENSION_PCENT_OVER_ROW_TOTAL:
933 return percentage_of_absolute_row;
935 case RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL:
936 return percentage_of_incremental_row;
942 // ----------------------------------------------------------------------------
943 // FAST NUMBER TO STRING
945 static void strreverse(char* begin, char* end)
949 aux = *end, *end-- = *begin, *begin++ = aux;
953 // ----------------------------------------------------------------------------
956 #define NETDATA_MEMORY_MODE_RAM_NAME "ram"
957 #define NETDATA_MEMORY_MODE_MAP_NAME "map"
958 #define NETDATA_MEMORY_MODE_SAVE_NAME "save"
960 #define NETDATA_MEMORY_MODE_RAM 0
961 #define NETDATA_MEMORY_MODE_MAP 1
962 #define NETDATA_MEMORY_MODE_SAVE 2
964 int memory_mode = NETDATA_MEMORY_MODE_SAVE;
966 const char *memory_mode_name(int id)
968 static const char ram[] = NETDATA_MEMORY_MODE_RAM_NAME;
969 static const char map[] = NETDATA_MEMORY_MODE_MAP_NAME;
970 static const char save[] = NETDATA_MEMORY_MODE_SAVE_NAME;
973 case NETDATA_MEMORY_MODE_RAM:
976 case NETDATA_MEMORY_MODE_MAP:
979 case NETDATA_MEMORY_MODE_SAVE:
987 int memory_mode_id(const char *name)
989 if(!strcmp(name, NETDATA_MEMORY_MODE_RAM_NAME))
990 return NETDATA_MEMORY_MODE_RAM;
991 else if(!strcmp(name, NETDATA_MEMORY_MODE_MAP_NAME))
992 return NETDATA_MEMORY_MODE_MAP;
994 return NETDATA_MEMORY_MODE_SAVE;
997 void *mymmap(const char *filename, unsigned long size)
999 if(memory_mode == NETDATA_MEMORY_MODE_RAM) return NULL;
1004 if(memory_mode == NETDATA_MEMORY_MODE_MAP)
1007 flags = MAP_PRIVATE;
1010 fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
1012 if(lseek(fd, size, SEEK_SET) == (long)size) {
1013 if(write(fd, "", 1) == 1) {
1015 if(ftruncate(fd, size))
1016 error("Cannot truncate file '%s' to size %ld. Will use the larger file.", filename, size);
1018 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
1020 if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK|MADV_WILLNEED) != 0)
1021 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
1024 else error("Cannot write to file '%s' at position %ld.", filename, size);
1026 else error("Cannot seek file '%s' to size %ld.", filename, size);
1030 else error("Cannot create/open file '%s'.", filename);
1035 int savememory(const char *filename, void *mem, unsigned long size)
1037 char tmpfilename[FILENAME_MAX + 1];
1039 snprintf(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
1041 int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
1043 error("Cannot create/open file '%s'.", filename);
1047 if(write(fd, mem, size) != (long)size) {
1048 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
1056 if(rename(tmpfilename, filename)) {
1057 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
1065 // ----------------------------------------------------------------------------
1068 #define RRD_STATS_NAME_MAX 1024
1070 typedef long double calculated_number;
1071 #define CALCULATED_NUMBER_FORMAT "%0.3Lf"
1072 //typedef long long calculated_number;
1073 //#define CALCULATED_NUMBER_FORMAT "%lld"
1075 typedef long long collected_number;
1076 #define COLLECTED_NUMBER_FORMAT "%lld"
1078 typedef long long total_number;
1079 #define TOTAL_NUMBER_FORMAT "%lld"
1081 typedef int32_t storage_number;
1082 typedef uint32_t ustorage_number;
1083 #define STORAGE_NUMBER_FORMAT "%d"
1085 #define RRD_STATS_MAGIC "NETDATA CACHE STATS FILE V007"
1086 #define RRD_DIMENSION_MAGIC "NETDATA CACHE DIMENSION FILE V005"
1088 struct rrd_dimension {
1089 char magic[sizeof(RRD_DIMENSION_MAGIC) + 1];// our magic
1090 char id[RRD_STATS_NAME_MAX + 1]; // the id of this dimension (for internal identification)
1091 char *name; // the name of this dimension (as presented to user)
1092 char cache_file[FILENAME_MAX+1];
1094 unsigned long hash; // a simple hash on the id, to speed up searching
1095 // we first compare hashes, and only if the hashes are equal we do string comparisons
1097 long entries; // how many entries this dimension has
1098 // this should be the same to the entries of the data set
1099 long current_entry; // the entry that is currently being updated
1101 int hidden; // if set to non zero, this dimension will not be sent to the client
1102 int mapped; // 1 if the file is mapped
1103 unsigned long memsize; // the memory allocated for this dimension
1109 struct timeval last_collected; // when was this dimension last updated
1111 calculated_number calculated_value;
1112 calculated_number last_calculated_value;
1114 collected_number collected_value;
1115 collected_number last_collected_value;
1117 struct rrd_dimension *next; // linking of dimensions within the same data set
1119 storage_number values[]; // the array of values - THIS HAS TO BE THE LAST MEMBER
1121 typedef struct rrd_dimension RRD_DIMENSION;
1124 char magic[sizeof(RRD_STATS_MAGIC) + 1];// our magic
1126 char id[RRD_STATS_NAME_MAX + 1]; // id of the data set
1127 char *name; // name of the data set
1128 char *cache_dir; // the directory to store dimension maps
1129 char cache_file[FILENAME_MAX+1];
1131 char *type; // the type of graph RRD_TYPE_* (a category, for determining graphing options)
1132 char *family; // the family of this data set (for grouping them together)
1133 char *title; // title shown to user
1134 char *units; // units of measurement
1136 pthread_rwlock_t rwlock;
1137 unsigned long counter; // the number of times we added values to this rrd
1138 unsigned long counter_done; // the number of times we added values to this rrd
1140 int mapped; // if set to 1, this is memory mapped
1141 unsigned long memsize; // how much mem we have allocated for this (without dimensions)
1143 unsigned long hash_name; // a simple hash on the name
1144 unsigned long hash; // a simple hash on the id, to speed up searching
1145 // we first compare hashes, and only if the hashes are equal we do string comparisons
1149 long entries; // total number of entries in the data set
1150 long current_entry; // the entry that is currently being updated
1151 // it goes around in a round-robin fashion
1153 int update_every; // every how many seconds is this updated?
1154 unsigned long long first_entry_t; // the timestamp (in microseconds) of the oldest entry in the db
1155 struct timeval last_updated; // when this data set was last updated
1156 struct timeval last_collected;
1157 unsigned long long usec_since_last_update;
1159 total_number absolute_total;
1160 total_number last_absolute_total;
1165 int isdetail; // if set, the data set should be considered as a detail of another
1166 // (the master data set should be the one that has the same family and is not detail)
1168 RRD_DIMENSION *dimensions; // the actual data for every dimension
1170 struct rrd_stats *next; // linking of rrd stats
1172 typedef struct rrd_stats RRD_STATS;
1174 RRD_STATS *root = NULL;
1175 pthread_rwlock_t root_rwlock = PTHREAD_RWLOCK_INITIALIZER;
1177 char *rrd_stats_strncpy_name(char *to, const char *from, int length)
1180 for(i = 0; i < length && from[i] ;i++) {
1181 if(from[i] == '.' || isalpha(from[i]) || isdigit(from[i])) to[i] = from[i];
1184 if(i < length) to[i] = '\0';
1185 to[length - 1] = '\0';
1190 void rrd_stats_set_name(RRD_STATS *st, const char *name)
1192 char b[CONFIG_MAX_VALUE + 1];
1193 char n[RRD_STATS_NAME_MAX + 1];
1195 snprintf(n, RRD_STATS_NAME_MAX, "%s.%s", st->type, name);
1196 rrd_stats_strncpy_name(b, n, CONFIG_MAX_VALUE);
1197 st->name = config_get(st->id, "name", b);
1198 st->hash_name = simple_hash(st->name);
1201 char *rrd_stats_cache_dir(const char *id)
1205 static char *cache_dir = NULL;
1206 if(!cache_dir) cache_dir = config_get("global", "database directory", "cache");
1208 char b[FILENAME_MAX + 1];
1209 char n[FILENAME_MAX + 1];
1210 rrd_stats_strncpy_name(b, id, FILENAME_MAX);
1212 snprintf(n, FILENAME_MAX, "%s/%s", cache_dir, b);
1213 ret = config_get(id, "database directory", n);
1215 if(memory_mode == NETDATA_MEMORY_MODE_MAP || memory_mode == NETDATA_MEMORY_MODE_SAVE) {
1216 int r = mkdir(ret, 0775);
1217 if(r != 0 && errno != EEXIST)
1218 error("Cannot create directory '%s'", ret);
1224 RRD_STATS *rrd_stats_create(const char *type, const char *id, const char *name, const char *family, const char *title, const char *units, long priority, int update_every, int chart_type)
1227 fatal("Cannot create rrd stats without an id.");
1231 char fullid[RRD_STATS_NAME_MAX + 1];
1232 char fullfilename[FILENAME_MAX + 1];
1235 snprintf(fullid, RRD_STATS_NAME_MAX, "%s.%s", type, id);
1237 long entries = config_get_number(fullid, "history", save_history);
1238 if(entries < 5) entries = config_set_number(fullid, "history", 5);
1239 if(entries > HISTORY_MAX) entries = config_set_number(fullid, "history", HISTORY_MAX);
1241 int enabled = config_get_boolean(fullid, "enabled", 1);
1242 if(!enabled) entries = 5;
1244 unsigned long size = sizeof(RRD_STATS);
1245 char *cache_dir = rrd_stats_cache_dir(fullid);
1247 debug(D_RRD_STATS, "Creating RRD_STATS for '%s.%s'.", type, id);
1249 snprintf(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
1250 st = (RRD_STATS *)mymmap(fullfilename, size);
1252 if(strcmp(st->magic, RRD_STATS_MAGIC) != 0) {
1254 error("File %s does not have our version. Clearing it.", fullfilename);
1257 else if(strcmp(st->id, fullid) != 0) {
1259 error("File %s does not have our id. Unmapping it.", fullfilename);
1263 else if(st->memsize != size || st->entries != entries) {
1265 error("File %s does not have the desired size. Clearing it.", fullfilename);
1268 else if(st->update_every != update_every) {
1270 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
1281 st->dimensions = NULL;
1283 st->mapped = memory_mode;
1286 st = calloc(1, size);
1288 fatal("Cannot allocate memory for RRD_STATS %s.%s", type, id);
1291 st->mapped = NETDATA_MEMORY_MODE_RAM;
1294 st->entries = entries;
1295 st->update_every = update_every;
1297 strcpy(st->cache_file, fullfilename);
1298 strcpy(st->magic, RRD_STATS_MAGIC);
1300 strcpy(st->id, fullid);
1301 st->hash = simple_hash(st->id);
1303 st->cache_dir = cache_dir;
1305 st->family = config_get(st->id, "family", family?family:st->id);
1306 st->units = config_get(st->id, "units", units?units:"");
1307 st->type = config_get(st->id, "type", type);
1308 st->chart_type = chart_type_id(config_get(st->id, "chart type", chart_type_name(chart_type)));
1310 if(name && *name) rrd_stats_set_name(st, name);
1311 else rrd_stats_set_name(st, id);
1314 char varvalue[CONFIG_MAX_VALUE + 1];
1315 snprintf(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
1316 st->title = config_get(st->id, "title", varvalue);
1319 st->priority = config_get_number(st->id, "priority", priority);
1320 st->enabled = enabled;
1324 st->last_collected.tv_sec = 0;
1325 st->last_collected.tv_usec = 0;
1327 pthread_rwlock_init(&st->rwlock, NULL);
1328 pthread_rwlock_wrlock(&root_rwlock);
1333 pthread_rwlock_unlock(&root_rwlock);
1338 RRD_DIMENSION *rrd_stats_dimension_add(RRD_STATS *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
1340 char filename[FILENAME_MAX + 1];
1341 char fullfilename[FILENAME_MAX + 1];
1343 char varname[CONFIG_MAX_NAME + 1];
1345 unsigned long size = sizeof(RRD_DIMENSION) + (st->entries * sizeof(storage_number));
1347 debug(D_RRD_STATS, "Adding dimension '%s/%s'.", st->id, id);
1349 rrd_stats_strncpy_name(filename, id, FILENAME_MAX);
1350 snprintf(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
1351 rd = (RRD_DIMENSION *)mymmap(fullfilename, size);
1353 if(strcmp(rd->magic, RRD_DIMENSION_MAGIC) != 0) {
1355 error("File %s does not have our version. Clearing it.", fullfilename);
1358 else if(rd->memsize != size) {
1360 error("File %s does not have the desired size. Clearing it.", fullfilename);
1363 else if(rd->multiplier != multiplier) {
1365 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
1368 else if(rd->divisor != divisor) {
1370 error("File %s does not have the same divisor. Clearing it.", fullfilename);
1373 else if(rd->algorithm != algorithm) {
1375 error("File %s does not have the same algorithm. Clearing it.", fullfilename);
1378 else if(strcmp(rd->id, id) != 0) {
1380 error("File %s does not have our dimension id. Unmapping it.", fullfilename);
1387 // we have a file mapped for rd
1388 rd->mapped = memory_mode;
1394 // if we didn't manage to get a mmap'd dimension, just create one
1396 rd = calloc(1, size);
1398 fatal("Cannot allocate RRD_DIMENSION %s/%s.", st->id, id);
1402 rd->mapped = NETDATA_MEMORY_MODE_RAM;
1406 strcpy(rd->magic, RRD_DIMENSION_MAGIC);
1407 strcpy(rd->cache_file, fullfilename);
1408 strncpy(rd->id, id, RRD_STATS_NAME_MAX);
1409 rd->hash = simple_hash(rd->id);
1411 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
1412 rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
1414 snprintf(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
1415 rd->algorithm = algorithm_id(config_get(st->id, varname, algorithm_name(algorithm)));
1417 snprintf(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
1418 rd->multiplier = config_get_number(st->id, varname, multiplier);
1420 snprintf(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
1421 rd->divisor = config_get_number(st->id, varname, divisor);
1422 if(!rd->divisor) rd->divisor = 1;
1424 rd->entries = st->entries;
1426 // append this dimension
1428 st->dimensions = rd;
1430 RRD_DIMENSION *td = st->dimensions;
1431 for(; td->next; td = td->next) ;
1438 void rrd_stats_dimension_set_name(RRD_STATS *st, RRD_DIMENSION *rd, const char *name)
1440 char varname[CONFIG_MAX_NAME + 1];
1441 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
1442 config_get(st->id, varname, name);
1445 void rrd_stats_dimension_free(RRD_DIMENSION *rd)
1447 if(rd->next) rrd_stats_dimension_free(rd->next);
1448 // free(rd->annotations);
1449 if(rd->mapped == NETDATA_MEMORY_MODE_SAVE) {
1450 debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_file);
1451 savememory(rd->cache_file, rd, rd->memsize);
1453 debug(D_RRD_STATS, "Unmapping dimension '%s'.", rd->name);
1454 munmap(rd, rd->memsize);
1456 else if(rd->mapped == NETDATA_MEMORY_MODE_MAP) {
1457 debug(D_RRD_STATS, "Unmapping dimension '%s'.", rd->name);
1458 munmap(rd, rd->memsize);
1461 debug(D_RRD_STATS, "Removing dimension '%s'.", rd->name);
1466 void rrd_stats_free_all(void)
1468 info("Freeing all memory...");
1471 for(st = root; st ;) {
1472 RRD_STATS *next = st->next;
1474 if(st->dimensions) rrd_stats_dimension_free(st->dimensions);
1475 st->dimensions = NULL;
1477 if(st->mapped == NETDATA_MEMORY_MODE_SAVE) {
1478 debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_file);
1479 savememory(st->cache_file, st, st->memsize);
1481 debug(D_RRD_STATS, "Unmapping stats '%s'.", st->name);
1482 munmap(st, st->memsize);
1484 else if(st->mapped == NETDATA_MEMORY_MODE_MAP) {
1485 debug(D_RRD_STATS, "Unmapping stats '%s'.", st->name);
1486 munmap(st, st->memsize);
1495 info("Memory cleanup completed...");
1498 void rrd_stats_save_all(void)
1503 pthread_rwlock_wrlock(&root_rwlock);
1504 for(st = root; st ; st = st->next) {
1505 pthread_rwlock_wrlock(&st->rwlock);
1507 if(st->mapped == NETDATA_MEMORY_MODE_SAVE) {
1508 debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_file);
1509 savememory(st->cache_file, st, st->memsize);
1512 for(rd = st->dimensions; rd ; rd = rd->next) {
1513 if(rd->mapped == NETDATA_MEMORY_MODE_SAVE) {
1514 debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_file);
1515 savememory(rd->cache_file, rd, rd->memsize);
1519 pthread_rwlock_unlock(&st->rwlock);
1521 pthread_rwlock_unlock(&root_rwlock);
1525 RRD_STATS *rrd_stats_find(const char *id)
1527 unsigned long hash = simple_hash(id);
1529 pthread_rwlock_rdlock(&root_rwlock);
1530 RRD_STATS *st = root;
1531 for ( ; st ; st = st->next )
1532 if(hash == st->hash)
1533 if(strcmp(st->id, id) == 0)
1535 pthread_rwlock_unlock(&root_rwlock);
1540 RRD_STATS *rrd_stats_find_bytype(const char *type, const char *id)
1542 char buf[RRD_STATS_NAME_MAX + 1];
1544 strncpy(buf, type, RRD_STATS_NAME_MAX - 1);
1545 buf[RRD_STATS_NAME_MAX - 1] = '\0';
1547 int len = strlen(buf);
1548 strncpy(&buf[len], id, RRD_STATS_NAME_MAX - len);
1549 buf[RRD_STATS_NAME_MAX] = '\0';
1551 return(rrd_stats_find(buf));
1554 RRD_STATS *rrd_stats_find_byname(const char *name)
1556 char b[CONFIG_MAX_VALUE + 1];
1558 rrd_stats_strncpy_name(b, name, CONFIG_MAX_VALUE);
1559 unsigned long hash = simple_hash(b);
1561 pthread_rwlock_rdlock(&root_rwlock);
1562 RRD_STATS *st = root;
1563 for ( ; st ; st = st->next ) {
1564 if(hash == st->hash_name && strcmp(st->name, b) == 0) break;
1566 pthread_rwlock_unlock(&root_rwlock);
1571 RRD_DIMENSION *rrd_stats_dimension_find(RRD_STATS *st, const char *id)
1573 unsigned long hash = simple_hash(id);
1575 RRD_DIMENSION *rd = st->dimensions;
1577 for ( ; rd ; rd = rd->next )
1578 if(hash == rd->hash)
1579 if(strcmp(rd->id, id) == 0)
1585 int rrd_stats_dimension_hide(RRD_STATS *st, const char *id)
1587 RRD_DIMENSION *rd = rrd_stats_dimension_find(st, id);
1589 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
1597 void rrd_stats_dimension_set_by_pointer(RRD_STATS *st, RRD_DIMENSION *rd, collected_number value)
1599 if(!st->last_collected.tv_sec) gettimeofday(&st->last_collected, NULL);
1601 rd->last_collected.tv_sec = st->last_collected.tv_sec;
1602 rd->last_collected.tv_usec = st->last_collected.tv_usec;
1604 rd->collected_value = value;
1607 int rrd_stats_dimension_set(RRD_STATS *st, char *id, collected_number value)
1609 RRD_DIMENSION *rd = rrd_stats_dimension_find(st, id);
1611 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
1615 rrd_stats_dimension_set_by_pointer(st, rd, value);
1619 void rrd_stats_next_internal(RRD_STATS *st)
1621 // a read lock is OK here
1622 pthread_rwlock_rdlock(&st->rwlock);
1625 for( rd = st->dimensions; rd ; rd = rd->next ) {
1626 rd->last_collected_value = rd->collected_value;
1627 rd->collected_value = 0;
1630 pthread_rwlock_unlock(&st->rwlock);
1633 void rrd_stats_next_timeval(RRD_STATS *st, struct timeval *now)
1635 if(!st->last_collected.tv_sec) {
1636 gettimeofday(&st->last_collected, NULL);
1637 unsigned long long ut = st->last_collected.tv_sec * 1000000ULL + st->last_collected.tv_usec - st->update_every * 1000000ULL;
1638 st->last_collected.tv_sec = ut / 1000000ULL;
1639 st->last_collected.tv_usec = ut % 1000000ULL;
1642 st->usec_since_last_update = usecdiff(now, &st->last_collected);
1643 st->last_collected.tv_sec = now->tv_sec;
1644 st->last_collected.tv_usec = now->tv_usec;
1646 rrd_stats_next_internal(st);
1649 void rrd_stats_next_usec(RRD_STATS *st, unsigned long long microseconds)
1651 if(!st->last_collected.tv_sec) {
1652 gettimeofday(&st->last_collected, NULL);
1653 unsigned long long ut = st->last_collected.tv_sec * 1000000ULL + st->last_collected.tv_usec;
1654 st->last_collected.tv_sec = ut / 1000000ULL;
1655 st->last_collected.tv_usec = ut % 1000000ULL;
1658 unsigned long long ut = st->last_collected.tv_sec * 1000000ULL + st->last_collected.tv_usec + microseconds;
1659 st->last_collected.tv_sec = ut / 1000000ULL;
1660 st->last_collected.tv_usec = ut % 1000000ULL;
1662 st->usec_since_last_update = microseconds;
1664 rrd_stats_next_internal(st);
1667 void rrd_stats_next(RRD_STATS *st)
1669 if(st->last_collected.tv_sec) {
1671 gettimeofday(&now, NULL);
1673 rrd_stats_next_timeval(st, &now);
1676 rrd_stats_next_usec(st, st->update_every * 1000000ULL);
1679 void rrd_stats_next_plugins(RRD_STATS *st)
1681 rrd_stats_next_usec(st, st->update_every * 1000000ULL);
1684 unsigned long long rrd_stats_done(RRD_STATS *st)
1686 RRD_DIMENSION *rd, *last;
1689 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0)
1690 error("Cannot set pthread cancel state to DISABLE.");
1692 // a read lock is OK here
1693 pthread_rwlock_rdlock(&st->rwlock);
1695 if(!st->last_updated.tv_sec) {
1696 unsigned long long ut = st->last_collected.tv_sec * 1000000ULL + st->last_collected.tv_usec - st->usec_since_last_update;
1697 st->last_updated.tv_sec = ut / 1000000ULL;
1698 st->last_updated.tv_usec = ut % 1000000ULL;
1701 unsigned long long last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
1702 unsigned long long now_ut = st->last_collected.tv_sec * 1000000ULL + st->last_collected.tv_usec + st->usec_since_last_update;
1703 unsigned long long next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
1706 if(st->counter_done == 1 || now_ut < next_ut) {
1707 if(st->debug) debug(D_RRD_STATS, "%s: Skipping collected values (usec since last update = %llu, counter_done = %lu)", st->name, st->usec_since_last_update, st->counter_done);
1708 // we don't have any usable data yet
1709 pthread_rwlock_unlock(&st->rwlock);
1711 if(pthread_setcancelstate(oldstate, NULL) != 0)
1712 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
1714 return(st->usec_since_last_update);
1717 if(st->debug) debug(D_RRD_STATS, "microseconds since last update: %llu", st->usec_since_last_update);
1719 // calculate totals and count the dimensions
1721 st->last_absolute_total = st->absolute_total;
1722 st->absolute_total = 0;
1723 for( rd = st->dimensions, dimensions = 0 ; rd ; rd = rd->next, dimensions++ )
1724 st->absolute_total += rd->collected_value;
1726 // process all dimensions to calculate its values
1727 for( rd = st->dimensions ; rd ; rd = rd->next ) {
1728 switch(rd->algorithm) {
1729 case RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL:
1730 // the percentage of the current increment
1731 // over the increment of all dimensions together
1732 if(st->absolute_total == st->last_absolute_total) rd->calculated_value = 0;
1733 else rd->calculated_value =
1734 (calculated_number)100
1735 * (calculated_number)(rd->collected_value - rd->last_collected_value)
1736 / (calculated_number)(st->absolute_total - st->last_absolute_total);
1739 debug(D_RRD_STATS, "%s/%s: CALC "
1740 CALCULATED_NUMBER_FORMAT " = 100"
1741 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
1742 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
1744 , rd->calculated_value
1745 , rd->collected_value, rd->last_collected_value
1746 , st->absolute_total, st->last_absolute_total
1750 case RRD_DIMENSION_PCENT_OVER_ROW_TOTAL:
1751 if(!st->absolute_total) rd->calculated_value = 0;
1753 // the percentage of the current value
1754 // over the total of all dimensions
1755 rd->calculated_value =
1756 (calculated_number)100
1757 * (calculated_number)rd->collected_value
1758 / (calculated_number)st->absolute_total;
1761 debug(D_RRD_STATS, "%s/%s: CALC "
1762 CALCULATED_NUMBER_FORMAT " = 100"
1763 " * " COLLECTED_NUMBER_FORMAT
1764 " / " COLLECTED_NUMBER_FORMAT
1766 , rd->calculated_value
1767 , rd->collected_value
1768 , st->absolute_total
1772 case RRD_DIMENSION_INCREMENTAL:
1773 // we need the incremental calculation to produce per second results
1774 // so, we multiply with 1.000.000 and divide by the microseconds passed since
1777 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
1778 // to reset the calculation (it will give zero as the calculation for this second)
1779 if(rd->last_collected_value > rd->collected_value) rd->last_collected_value = rd->collected_value;
1781 rd->calculated_value =
1782 (calculated_number)1000000
1783 * (calculated_number)(rd->collected_value - rd->last_collected_value)
1784 / (calculated_number)st->usec_since_last_update;
1787 debug(D_RRD_STATS, "%s/%s: CALC "
1788 CALCULATED_NUMBER_FORMAT " = 1000000"
1789 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
1792 , rd->calculated_value
1793 , rd->collected_value, rd->last_collected_value
1794 , st->usec_since_last_update
1798 case RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION:
1799 // we need the incremental calculation to produce per second results
1800 // so, we multiply with 1.000.000 and divide by the microseconds passed since
1803 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
1804 // to reset the calculation (it will give zero as the calculation for this second)
1805 if(rd->last_collected_value > rd->collected_value) rd->last_collected_value = rd->collected_value;
1807 rd->calculated_value = (calculated_number)(rd->collected_value - rd->last_collected_value);
1810 debug(D_RRD_STATS, "%s/%s: CALC "
1811 CALCULATED_NUMBER_FORMAT " = "
1812 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
1814 , rd->calculated_value
1815 , rd->collected_value, rd->last_collected_value
1819 case RRD_DIMENSION_ABSOLUTE:
1820 case RRD_DIMENSION_ABSOLUTE_NO_INTERPOLATION:
1821 rd->calculated_value = (calculated_number)rd->collected_value;
1824 debug(D_RRD_STATS, "%s/%s: CALC "
1825 CALCULATED_NUMBER_FORMAT " = "
1826 COLLECTED_NUMBER_FORMAT
1828 , rd->calculated_value
1829 , rd->collected_value
1834 // make the default zero, to make sure
1835 // it gets noticed when we add new types
1836 rd->calculated_value = 0;
1839 debug(D_RRD_STATS, "%s/%s: CALC "
1840 CALCULATED_NUMBER_FORMAT " = 0"
1842 , rd->calculated_value
1848 for( ; next_ut < now_ut ; next_ut += st->update_every * 1000000ULL ) {
1849 unsigned long long np = next_ut - last_ut;
1851 st->last_updated.tv_sec = next_ut / 1000000ULL;
1852 st->last_updated.tv_usec = 0;
1854 for( rd = st->dimensions ; rd ; rd = rd->next ) {
1855 calculated_number new_value;
1857 switch(rd->algorithm) {
1858 case RRD_DIMENSION_ABSOLUTE_NO_INTERPOLATION:
1859 case RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION:
1860 if(next_ut + st->update_every * 1000000ULL < now_ut)
1861 new_value = rd->last_calculated_value;
1863 new_value = rd->calculated_value;
1866 debug(D_RRD_STATS, "%s/%s: CALC2 "
1867 CALCULATED_NUMBER_FORMAT
1874 new_value = (calculated_number)
1875 ( ( (rd->calculated_value - rd->last_calculated_value)
1876 * (calculated_number)np
1877 / (calculated_number)(now_ut - last_ut)
1879 + rd->last_calculated_value
1883 debug(D_RRD_STATS, "%s/%s: CALC2 "
1884 CALCULATED_NUMBER_FORMAT " = ((("
1885 "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
1887 " / %llu) + " CALCULATED_NUMBER_FORMAT
1890 , rd->calculated_value, rd->last_calculated_value
1892 , (now_ut - last_ut), rd->last_calculated_value
1897 rd->values[st->current_entry] = (storage_number)
1899 * (calculated_number)10
1900 * (calculated_number)rd->multiplier
1901 / (calculated_number)rd->divisor
1905 debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
1906 STORAGE_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
1912 , rd->values[st->current_entry], new_value
1917 if((rd->algorithm == RRD_DIMENSION_ABSOLUTE_NO_INTERPOLATION || rd->algorithm == RRD_DIMENSION_ABSOLUTE_NO_INTERPOLATION)
1918 && (next_ut + st->update_every * 1000000ULL < now_ut)) {
1919 // there is another iteration
1920 // do not change the anything
1924 rd->last_calculated_value = rd->calculated_value = new_value;
1927 if(st->first_entry_t && st->counter >= (unsigned long long)st->entries) {
1928 // the db is overwriting values
1929 // add the value we will overwrite
1930 st->first_entry_t += st->update_every * 1000000ULL;
1934 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1935 if(!st->first_entry_t) st->first_entry_t = next_ut;
1939 // ALL DONE ABOUT THE DATA UPDATE
1940 // --------------------------------------------------------------------
1943 // find if there are any obsolete dimensions (not updated recently)
1944 for( rd = st->dimensions; rd ; rd = rd->next )
1945 if((rd->last_collected.tv_sec + (10 * st->update_every)) < st->last_collected.tv_sec)
1949 // there is dimension to free
1950 // upgrade our read lock to a write lock
1951 pthread_rwlock_unlock(&st->rwlock);
1952 pthread_rwlock_wrlock(&st->rwlock);
1954 for( rd = st->dimensions, last = NULL ; rd ; ) {
1955 if((rd->last_collected.tv_sec + (10 * st->update_every)) < st->last_collected.tv_sec) { // remove it only it is not updated in 10 seconds
1956 debug(D_RRD_STATS, "Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1959 st->dimensions = rd->next;
1961 rrd_stats_dimension_free(rd);
1962 rd = st->dimensions;
1966 last->next = rd->next;
1968 rrd_stats_dimension_free(rd);
1978 if(!st->dimensions) st->enabled = 0;
1981 pthread_rwlock_unlock(&st->rwlock);
1983 if(pthread_setcancelstate(oldstate, NULL) != 0)
1984 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
1986 return(st->usec_since_last_update);
1991 // ----------------------------------------------------------------------------
1995 long size; // allocation size of buffer
1996 long bytes; // current data length in buffer
1997 long sent; // current data length sent to output
1998 char *buffer; // the buffer
2000 long rbytes; // if non-zero, the excepted size of ifd
2001 time_t date; // the date this content has been generated
2004 #define web_buffer_printf(wb, args...) wb->bytes += snprintf(&wb->buffer[wb->bytes], (wb->size - wb->bytes), ##args)
2005 #define web_buffer_reset(wb) wb->buffer[wb->bytes = 0] = '\0'
2007 void web_buffer_strcpy(struct web_buffer *wb, const char *txt)
2009 char *buffer = wb->buffer;
2010 long bytes = wb->bytes, size = wb->size, i = 0;
2012 while(txt[i] && bytes < size)
2013 buffer[bytes++] = txt[i++];
2018 void web_buffer_rrd_value(struct web_buffer *wb, storage_number value)
2020 if(wb->size - wb->bytes < 11) return;
2022 char *str = &wb->buffer[wb->bytes];
2025 // make sure it is unsigned
2026 ustorage_number uvalue = (value < 0) ? -value : value;
2029 do *wstr++ = (char)(48 + (uvalue % 10)); while(uvalue /= 10);
2031 // if it is just one byte, add a zero
2032 if((wstr - str) == 1) *wstr++ = '0';
2034 // put the sign back
2035 if (value < 0) *wstr++ = '-';
2039 strreverse(str, wstr);
2042 // move the last digit one byte to the right
2045 // put the dot in the hole
2053 // update the buffer length
2054 wb->bytes += (wstr - str);
2057 // generate a javascript date, the fastest possible way...
2058 void web_buffer_jsdate(struct web_buffer *wb, int year, int month, int day, int hours, int minutes, int seconds)
2061 // 01234567890123456789012345678901234
2062 // Date(2014, 04, 01, 03, 28, 20, 065)
2064 if(wb->size - wb->bytes < 36) return;
2066 char *b = &wb->buffer[wb->bytes];
2074 b[i++]= 48 + year / 1000; year -= (year / 1000) * 1000;
2075 b[i++]= 48 + year / 100; year -= (year / 100) * 100;
2076 b[i++]= 48 + year / 10;
2077 b[i++]= 48 + year % 10;
2080 b[i]= 48 + month / 10; if(b[i] != '0') i++;
2081 b[i++]= 48 + month % 10;
2084 b[i]= 48 + day / 10; if(b[i] != '0') i++;
2085 b[i++]= 48 + day % 10;
2088 b[i]= 48 + hours / 10; if(b[i] != '0') i++;
2089 b[i++]= 48 + hours % 10;
2092 b[i]= 48 + minutes / 10; if(b[i] != '0') i++;
2093 b[i++]= 48 + minutes % 10;
2096 b[i]= 48 + seconds / 10; if(b[i] != '0') i++;
2097 b[i++]= 48 + seconds % 10;
2104 struct web_buffer *web_buffer_create(long size)
2106 struct web_buffer *b;
2108 debug(D_WEB_BUFFER, "Creating new web buffer of size %d.", size);
2110 b = calloc(1, sizeof(struct web_buffer));
2112 error("Cannot allocate a web_buffer.");
2116 b->buffer = malloc(size);
2118 error("Cannot allocate a buffer of size %u.", size);
2122 b->buffer[0] = '\0';
2124 b->contenttype = CT_TEXT_PLAIN;
2128 void web_buffer_free(struct web_buffer *b)
2130 debug(D_WEB_BUFFER, "Freeing web buffer of size %d.", b->size);
2132 if(b->buffer) free(b->buffer);
2136 void web_buffer_increase(struct web_buffer *b, long free_size_required)
2138 long left = b->size - b->bytes;
2140 if(left >= free_size_required) return;
2141 long increase = free_size_required - left;
2142 if(increase < WEB_DATA_LENGTH_INCREASE_STEP) increase = WEB_DATA_LENGTH_INCREASE_STEP;
2144 debug(D_WEB_BUFFER, "Increasing data buffer from size %d to %d.", b->size, b->size + increase);
2146 b->buffer = realloc(b->buffer, b->size + increase);
2147 if(!b->buffer) fatal("Failed to increase data buffer from size %d to %d.", b->size, b->size + increase);
2149 b->size += increase;
2152 #define WEB_CLIENT_MODE_NORMAL 0
2153 #define WEB_CLIENT_MODE_FILECOPY 1
2155 #define URL_MAX 8192
2158 unsigned long long id;
2159 char client_ip[101];
2160 char last_url[URL_MAX+1];
2162 struct timeval tv_in, tv_ready;
2167 struct sockaddr_in clientaddr;
2169 pthread_t thread; // the thread servicing this client
2170 int obsolete; // if set to 1, the listener will remove this client
2175 struct web_buffer *data;
2177 int zoutput; // if set to 1, web_client_send() will send compressed data
2178 z_stream zstream; // zlib stream for sending compressed output to client
2179 Bytef zbuffer[ZLIB_CHUNK]; // temporary buffer for storing compressed output
2180 long zsent; // the compressed bytes we have sent to the client
2181 long zhave; // the compressed bytes that we have to send
2187 char response_header[MAX_HTTP_HEADER_SIZE+1];
2189 struct web_client *prev;
2190 struct web_client *next;
2191 } *web_clients = NULL;
2193 unsigned long long web_clients_count = 0;
2195 struct web_client *web_client_create(int listener)
2197 struct web_client *w;
2200 w = calloc(1, sizeof(struct web_client));
2202 error("Cannot allocate new web_client memory.");
2206 w->id = ++web_clients_count;
2207 w->mode = WEB_CLIENT_MODE_NORMAL;
2209 addrlen = sizeof(w->clientaddr);
2210 w->ifd = accept(listener, (struct sockaddr *)&w->clientaddr, &addrlen);
2212 error("%llu: Cannot accept new incoming connection.", w->id);
2218 strncpy(w->client_ip, inet_ntoa(w->clientaddr.sin_addr), 100);
2219 w->client_ip[100] = '\0';
2221 debug(D_WEB_CLIENT_ACCESS, "%llu: New web client from %s on socket %d.", w->id, w->client_ip, w->ifd);
2225 if(setsockopt(w->ifd, SOL_SOCKET, SO_KEEPALIVE, (char *) &flag, sizeof(int)) != 0) error("%llu: Cannot set SO_KEEPALIVE on socket.", w->id);
2228 w->data = web_buffer_create(INITIAL_WEB_DATA_LENGTH);
2235 w->wait_receive = 1;
2237 if(web_clients) web_clients->prev = w;
2238 w->next = web_clients;
2241 global_statistics.connected_clients++;
2246 struct web_client *web_client_free(struct web_client *w)
2248 struct web_client *n = w->next;
2250 debug(D_WEB_CLIENT_ACCESS, "%llu: Closing web client from %s.", w->id, inet_ntoa(w->clientaddr.sin_addr));
2252 if(w->prev) w->prev->next = w->next;
2253 if(w->next) w->next->prev = w->prev;
2255 if(w == web_clients) web_clients = w->next;
2257 if(w->data) web_buffer_free(w->data);
2259 if(w->ofd != w->ifd) close(w->ofd);
2262 global_statistics.connected_clients--;
2267 #define GROUP_AVERAGE 0
2270 // find the oldest entry in the data, skipping all empty slots
2271 time_t rrd_stats_first_entry_t(RRD_STATS *st)
2273 if(!st->first_entry_t) return st->last_updated.tv_sec;
2275 return st->first_entry_t / 1000000;
2278 unsigned long rrd_stats_one_json(RRD_STATS *st, char *options, struct web_buffer *wb)
2280 time_t now = time(NULL);
2281 web_buffer_increase(wb, 16384);
2283 pthread_rwlock_rdlock(&st->rwlock);
2285 web_buffer_printf(wb,
2287 "\t\t\t\"id\": \"%s\",\n"
2288 "\t\t\t\"name\": \"%s\",\n"
2289 "\t\t\t\"type\": \"%s\",\n"
2290 "\t\t\t\"family\": \"%s\",\n"
2291 "\t\t\t\"title\": \"%s\",\n"
2292 "\t\t\t\"priority\": %ld,\n"
2293 "\t\t\t\"enabled\": %d,\n"
2294 "\t\t\t\"units\": \"%s\",\n"
2295 "\t\t\t\"url\": \"/data/%s/%s\",\n"
2296 "\t\t\t\"chart_type\": \"%s\",\n"
2297 "\t\t\t\"counter\": %ld,\n"
2298 "\t\t\t\"entries\": %ld,\n"
2299 "\t\t\t\"first_entry_t\": %lu,\n"
2300 "\t\t\t\"last_entry\": %ld,\n"
2301 "\t\t\t\"last_entry_t\": %lu,\n"
2302 "\t\t\t\"last_entry_secs_ago\": %lu,\n"
2303 "\t\t\t\"update_every\": %d,\n"
2304 "\t\t\t\"isdetail\": %d,\n"
2305 "\t\t\t\"usec_since_last_update\": %llu,\n"
2306 "\t\t\t\"absolute_total\": " TOTAL_NUMBER_FORMAT ",\n"
2307 "\t\t\t\"last_absolute_total\": " TOTAL_NUMBER_FORMAT ",\n"
2308 "\t\t\t\"dimensions\": [\n"
2317 , st->name, options?options:""
2318 , chart_type_name(st->chart_type)
2321 , rrd_stats_first_entry_t(st)
2323 , st->last_updated.tv_sec
2324 , now - (st->last_updated.tv_sec > now) ? now : st->last_updated.tv_sec
2327 , st->usec_since_last_update
2328 , st->absolute_total
2329 , st->last_absolute_total
2332 unsigned long memory = st->memsize;
2335 for(rd = st->dimensions; rd ; rd = rd->next) {
2336 memory += rd->memsize;
2338 web_buffer_printf(wb,
2340 "\t\t\t\t\t\"id\": \"%s\",\n"
2341 "\t\t\t\t\t\"name\": \"%s\",\n"
2342 "\t\t\t\t\t\"entries\": %ld,\n"
2343 "\t\t\t\t\t\"isHidden\": %d,\n"
2344 "\t\t\t\t\t\"algorithm\": \"%s\",\n"
2345 "\t\t\t\t\t\"multiplier\": %ld,\n"
2346 "\t\t\t\t\t\"divisor\": %ld,\n"
2347 "\t\t\t\t\t\"last_entry_t\": %lu,\n"
2348 "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
2349 "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
2350 "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n"
2351 "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n"
2352 "\t\t\t\t\t\"memory\": %lu\n"
2358 , algorithm_name(rd->algorithm)
2361 , rd->last_collected.tv_sec
2362 , rd->collected_value
2363 , rd->calculated_value
2364 , rd->last_collected_value
2365 , rd->last_calculated_value
2371 web_buffer_printf(wb,
2373 "\t\t\t\"memory\" : %lu\n"
2378 pthread_rwlock_unlock(&st->rwlock);
2382 #define RRD_GRAPH_JSON_HEADER "{\n\t\"charts\": [\n"
2383 #define RRD_GRAPH_JSON_FOOTER "\n\t]\n}\n"
2385 void rrd_stats_graph_json(RRD_STATS *st, char *options, struct web_buffer *wb)
2387 web_buffer_increase(wb, 16384);
2389 web_buffer_printf(wb, RRD_GRAPH_JSON_HEADER);
2390 rrd_stats_one_json(st, options, wb);
2391 web_buffer_printf(wb, RRD_GRAPH_JSON_FOOTER);
2394 void rrd_stats_all_json(struct web_buffer *wb)
2396 web_buffer_increase(wb, 1024);
2398 unsigned long memory = 0;
2402 web_buffer_printf(wb, RRD_GRAPH_JSON_HEADER);
2404 pthread_rwlock_rdlock(&root_rwlock);
2405 for(st = root, c = 0; st ; st = st->next) {
2407 if(c) web_buffer_printf(wb, "%s", ",\n");
2408 memory += rrd_stats_one_json(st, NULL, wb);
2412 pthread_rwlock_unlock(&root_rwlock);
2414 web_buffer_printf(wb, "\n\t],\n"
2415 "\t\"hostname\": \"%s\",\n"
2416 "\t\"update_every\": %d,\n"
2417 "\t\"history\": %d,\n"
2418 "\t\"memory\": %lu\n"
2427 unsigned long rrd_stats_json(int type, RRD_STATS *st, struct web_buffer *wb, int entries_to_show, int group, int group_method, time_t after, time_t before, int only_non_zero)
2430 pthread_rwlock_rdlock(&st->rwlock);
2433 // -------------------------------------------------------------------------
2434 // switch from JSON to google JSON
2439 case DATASOURCE_GOOGLE_JSON:
2440 case DATASOURCE_GOOGLE_JSONP:
2445 case DATASOURCE_JSON:
2451 // -------------------------------------------------------------------------
2452 // validate the parameters
2454 if(entries_to_show < 1) entries_to_show = 1;
2455 if(group < 1) group = 1;
2457 // make sure current_entry is within limits
2458 long current_entry = (long)st->current_entry - (long)1;
2459 if(current_entry < 0) current_entry = 0;
2460 else if(current_entry >= st->entries) current_entry = st->entries - 1;
2462 // find the oldest entry of the round-robin
2463 long max_entries_init = (st->counter < (unsigned long)st->entries) ? st->counter : (unsigned long)st->entries;
2465 if(before == 0) before = st->last_updated.tv_sec;
2466 if(after == 0) after = rrd_stats_first_entry_t(st);
2468 time_t time_init = st->last_updated.tv_sec;
2472 // our return value (the last timestamp printed)
2473 // this is required to detect re-transmit in google JSONP
2474 time_t last_timestamp = 0;
2477 // -------------------------------------------------------------------------
2478 // find how many dimensions we have
2482 for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
2484 pthread_rwlock_unlock(&st->rwlock);
2485 web_buffer_printf(wb, "No dimensions yet.");
2490 // -------------------------------------------------------------------------
2491 // prepare various strings, to speed up the loop
2493 char overflow_annotation[201]; snprintf(overflow_annotation, 200, ",{%sv%s:%sRESET OR OVERFLOW%s},{%sv%s:%sThe counters have been wrapped.%s}", kq, kq, sq, sq, kq, kq, sq, sq);
2494 char normal_annotation[201]; snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
2495 char pre_date[51]; snprintf(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
2496 char post_date[21]; snprintf(post_date, 20, "%s}", sq);
2497 char pre_value[21]; snprintf(pre_value, 20, ",{%sv%s:", kq, kq);
2498 char post_value[21]; snprintf(post_value, 20, "}");
2501 // -------------------------------------------------------------------------
2502 // checks for debuging
2505 debug(D_RRD_STATS, "%s first_entry_t = %lu, last_entry_t = %lu, duration = %lu, after = %lu, before = %lu, duration = %lu, entries_to_show = %lu, group = %lu, max_entries = %ld"
2507 , rrd_stats_first_entry_t(st)
2508 , st->last_updated.tv_sec
2509 , st->last_updated.tv_sec - rrd_stats_first_entry_t(st)
2519 debug(D_RRD_STATS, "WARNING: %s The newest value in the database (%lu) is earlier than the oldest (%lu)", st->name, before, after);
2521 if((before - after) > st->entries * st->update_every)
2522 debug(D_RRD_STATS, "WARNING: %s The time difference between the oldest and the newest entries (%lu) is higher than the capacity of the database (%lu)", st->name, before - after, st->entries * st->update_every);
2526 // -------------------------------------------------------------------------
2527 // temp arrays for keeping values per dimension
2529 calculated_number group_values[dimensions]; // keep sums when grouping
2530 storage_number print_values[dimensions]; // keep the final value to be printed
2531 int print_hidden[dimensions]; // keep hidden flags
2532 int found_non_zero[dimensions];
2535 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2536 group_values[c] = print_values[c] = 0;
2537 print_hidden[c] = rd->hidden;
2538 found_non_zero[c] = 0;
2541 // -------------------------------------------------------------------------
2542 // remove dimensions that contain only zeros
2545 if(only_non_zero) max_loop = 2;
2547 for(; max_loop ; max_loop--) {
2549 // -------------------------------------------------------------------------
2550 // print the JSON header
2552 web_buffer_printf(wb, "{\n %scols%s:\n [\n", kq, kq);
2553 web_buffer_printf(wb, " {%sid%s:%s%s,%slabel%s:%stime%s,%spattern%s:%s%s,%stype%s:%sdatetime%s},\n", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq);
2554 web_buffer_printf(wb, " {%sid%s:%s%s,%slabel%s:%s%s,%spattern%s:%s%s,%stype%s:%sstring%s,%sp%s:{%srole%s:%sannotation%s}},\n", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, kq, kq, sq, sq);
2555 web_buffer_printf(wb, " {%sid%s:%s%s,%slabel%s:%s%s,%spattern%s:%s%s,%stype%s:%sstring%s,%sp%s:{%srole%s:%sannotationText%s}}", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, kq, kq, sq, sq);
2557 // print the header for each dimension
2558 // and update the print_hidden array for the dimensions that should be hidden
2560 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2561 if(!print_hidden[c]) {
2563 web_buffer_printf(wb, ",\n {%sid%s:%s%s,%slabel%s:%s%s%s,%spattern%s:%s%s,%stype%s:%snumber%s}", kq, kq, sq, sq, kq, kq, sq, rd->name, sq, kq, kq, sq, sq, kq, kq, sq, sq);
2567 web_buffer_printf(wb, ",\n {%sid%s:%s%s,%slabel%s:%s%s%s,%spattern%s:%s%s,%stype%s:%snumber%s}", kq, kq, sq, sq, kq, kq, sq, "no data", sq, kq, kq, sq, sq, kq, kq, sq, sq);
2570 // print the begin of row data
2571 web_buffer_printf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
2574 // -------------------------------------------------------------------------
2577 int annotate_reset = 0;
2578 int annotation_count = 0;
2580 // to allow grouping on the same values, we need a pad
2581 long pad = before % group;
2583 // the minimum line length we expect
2584 int line_size = 4096 + (dimensions * 200);
2586 time_t now = time_init;
2587 long max_entries = max_entries_init;
2591 long count = 0, printed = 0, group_count = 0;
2593 for(t = current_entry; max_entries ; now--, t--, max_entries--) {
2594 if(t < 0) t = st->entries - 1;
2599 debug(D_RRD_STATS, "%s t = %ld, count = %ld, group_count = %ld, printed = %ld, now = %lu, %s %s"
2606 , (((count + 1 - pad) % group) == 0)?"PRINT":" - "
2607 , (now >= after && now <= before)?"RANGE":" - "
2611 // make sure we return data in the proper time range
2612 if(now < after || now > before) continue;
2617 if(((count - pad) % group) == 0) {
2618 if(printed >= entries_to_show) {
2619 // debug(D_RRD_STATS, "Already printed all rows. Stopping.");
2623 if(group_count != group) {
2624 // this is an incomplete group, skip it.
2625 for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++)
2626 group_values[c] = 0;
2632 // check if we may exceed the buffer provided
2633 web_buffer_increase(wb, line_size);
2635 // generate the local date time
2636 struct tm *tm = localtime(&now);
2637 if(!tm) { error("localtime() failed."); continue; }
2638 if(now > last_timestamp) last_timestamp = now;
2640 if(printed) web_buffer_strcpy(wb, "]},\n");
2641 web_buffer_strcpy(wb, pre_date);
2642 web_buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
2643 web_buffer_strcpy(wb, post_date);
2648 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2649 long value = rd->values[t];
2651 switch(group_method) {
2653 if(abs(value) > abs(group_values[c])) group_values[c] = value;
2658 group_values[c] += value;
2659 if(print_this) group_values[c] /= group_count;
2664 print_values[c] = group_values[c];
2665 group_values[c] = 0;
2672 if(annotate_reset) {
2674 web_buffer_strcpy(wb, overflow_annotation);
2678 web_buffer_strcpy(wb, normal_annotation);
2681 for(c = 0 ; c < dimensions ; c++) {
2682 if(!print_hidden[c]) {
2684 web_buffer_strcpy(wb, pre_value);
2685 web_buffer_rrd_value(wb, print_values[c]);
2686 web_buffer_strcpy(wb, post_value);
2688 if(print_values[c]) found_non_zero[c]++;
2692 web_buffer_strcpy(wb, pre_value);
2693 web_buffer_rrd_value(wb, (storage_number)0);
2694 web_buffer_strcpy(wb, post_value);
2701 if(printed) web_buffer_printf(wb, "]}");
2702 web_buffer_printf(wb, "\n ]\n}\n");
2704 if(only_non_zero && max_loop > 1) {
2706 for(rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
2707 group_values[c] = 0;
2709 if(!print_hidden[c] && !found_non_zero[c]) {
2711 print_hidden[c] = 1;
2715 if(changed) web_buffer_reset(wb);
2722 debug(D_RRD_STATS, "RRD_STATS_JSON: %s total %ld bytes", st->name, wb->bytes);
2724 pthread_rwlock_unlock(&st->rwlock);
2725 return last_timestamp;
2728 void generate_config(struct web_buffer *wb, int only_changed)
2732 struct config_value *cv;
2734 for(i = 0; i < 3 ;i++) {
2735 web_buffer_increase(wb, 500);
2738 web_buffer_printf(wb,
2739 "# NetData Configuration\n"
2740 "# You can uncomment and change any of the options bellow.\n"
2741 "# The value shown in the commented settings, is the default value.\n"
2742 "\n# global netdata configuration\n");
2746 web_buffer_printf(wb, "\n\n# per plugin configuration\n");
2750 web_buffer_printf(wb, "\n\n# per chart configuration\n");
2754 for(co = config_root; co ; co = co->next) {
2755 if(strcmp(co->name, "global") == 0 || strcmp(co->name, "plugins") == 0) pri = 0;
2756 else if(strncmp(co->name, "plugin:", 7) == 0) pri = 1;
2763 for(cv = co->values; cv ; cv = cv->next) {
2765 changed += cv->changed;
2769 if(!count) continue;
2770 if(only_changed && !changed) continue;
2773 web_buffer_increase(wb, 500);
2774 web_buffer_printf(wb, "\n# node '%s' is not used.", co->name);
2777 web_buffer_increase(wb, CONFIG_MAX_NAME + 4);
2778 web_buffer_printf(wb, "\n[%s]\n", co->name);
2780 for(cv = co->values; cv ; cv = cv->next) {
2782 if(used && !cv->used) {
2783 web_buffer_increase(wb, CONFIG_MAX_NAME + 200);
2784 web_buffer_printf(wb, "\n\t# option '%s' is not used.\n", cv->name);
2786 web_buffer_increase(wb, CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 5);
2787 web_buffer_printf(wb, "\t%s%s = %s\n", (!cv->changed && cv->used)?"# ":"", cv->name, cv->value);
2794 int mysendfile(struct web_client *w, char *filename)
2796 static char *web_dir = NULL;
2797 if(!web_dir) web_dir = config_get("global", "web files directory", "web");
2799 debug(D_WEB_CLIENT, "%llu: Looking for file '%s'...", w->id, filename);
2801 // skip leading slashes
2802 while (*filename == '/') filename++;
2804 // if the filename contain known paths, skip them
2805 if(strncmp(filename, WEB_PATH_DATA "/", strlen(WEB_PATH_DATA) + 1) == 0) filename = &filename[strlen(WEB_PATH_DATA) + 1];
2806 else if(strncmp(filename, WEB_PATH_DATASOURCE "/", strlen(WEB_PATH_DATASOURCE) + 1) == 0) filename = &filename[strlen(WEB_PATH_DATASOURCE) + 1];
2807 else if(strncmp(filename, WEB_PATH_GRAPH "/", strlen(WEB_PATH_GRAPH) + 1) == 0) filename = &filename[strlen(WEB_PATH_GRAPH) + 1];
2808 else if(strncmp(filename, WEB_PATH_FILE "/", strlen(WEB_PATH_FILE) + 1) == 0) filename = &filename[strlen(WEB_PATH_FILE) + 1];
2810 // if the filename contains a / or a .., refuse to serve it
2811 if(strchr(filename, '/') != 0 || strstr(filename, "..") != 0) {
2812 debug(D_WEB_CLIENT_ACCESS, "%llu: File '%s' is not acceptable.", w->id, filename);
2813 web_buffer_printf(w->data, "File '%s' cannot be served. Filenames cannot contain / or ..", filename);
2818 char webfilename[FILENAME_MAX + 1];
2819 snprintf(webfilename, FILENAME_MAX, "%s/%s", web_dir, filename);
2821 // check if the file exists
2823 if(lstat(webfilename, &stat) != 0) {
2824 error("%llu: File '%s' is not found.", w->id, webfilename);
2825 web_buffer_printf(w->data, "File '%s' does not exist, or is not accessible.", filename);
2829 // check if the file is owned by us
2830 if(stat.st_uid != getuid() && stat.st_uid != geteuid()) {
2831 error("%llu: File '%s' is owned by user %d (I run as user %d). Access Denied.", w->id, webfilename, stat.st_uid, getuid());
2832 web_buffer_printf(w->data, "Access to file '%s' is not permitted.", filename);
2837 w->ifd = open(webfilename, O_NONBLOCK, O_RDONLY);
2841 if(errno == EBUSY || errno == EAGAIN) {
2842 error("%llu: File '%s' is busy, sending 307 Moved Temporarily to force retry.", w->id, webfilename);
2843 snprintf(w->response_header, MAX_HTTP_HEADER_SIZE, "Location: /" WEB_PATH_FILE "/%s\r\n", filename);
2844 web_buffer_printf(w->data, "The file '%s' is currently busy. Please try again later.", filename);
2848 error("%llu: Cannot open file '%s'.", w->id, webfilename);
2849 web_buffer_printf(w->data, "Cannot open file '%s'.", filename);
2854 // pick a Content-Type for the file
2855 if(strstr(filename, ".html") != NULL) w->data->contenttype = CT_TEXT_HTML;
2856 else if(strstr(filename, ".js") != NULL) w->data->contenttype = CT_APPLICATION_X_JAVASCRIPT;
2857 else if(strstr(filename, ".css") != NULL) w->data->contenttype = CT_TEXT_CSS;
2858 else if(strstr(filename, ".xml") != NULL) w->data->contenttype = CT_TEXT_XML;
2859 else if(strstr(filename, ".xsl") != NULL) w->data->contenttype = CT_TEXT_XSL;
2860 else if(strstr(filename, ".txt") != NULL) w->data->contenttype = CT_TEXT_PLAIN;
2861 else if(strstr(filename, ".svg") != NULL) w->data->contenttype = CT_IMAGE_SVG_XML;
2862 else if(strstr(filename, ".ttf") != NULL) w->data->contenttype = CT_APPLICATION_X_FONT_TRUETYPE;
2863 else if(strstr(filename, ".otf") != NULL) w->data->contenttype = CT_APPLICATION_X_FONT_OPENTYPE;
2864 else if(strstr(filename, ".woff") != NULL) w->data->contenttype = CT_APPLICATION_FONT_WOFF;
2865 else if(strstr(filename, ".eot") != NULL) w->data->contenttype = CT_APPLICATION_VND_MS_FONTOBJ;
2866 else w->data->contenttype = CT_APPLICATION_OCTET_STREAM;
2868 debug(D_WEB_CLIENT_ACCESS, "%llu: Sending file '%s' (%ld bytes, ifd %d, ofd %d).", w->id, webfilename, stat.st_size, w->ifd, w->ofd);
2870 w->mode = WEB_CLIENT_MODE_FILECOPY;
2871 w->wait_receive = 1;
2874 w->data->buffer[0] = '\0';
2875 w->data->rbytes = stat.st_size;
2876 w->data->date = stat.st_mtim.tv_sec;
2881 char *mystrsep(char **ptr, char *s)
2884 while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
2888 void web_client_reset(struct web_client *w)
2891 gettimeofday(&tv, NULL);
2893 long sent = w->zoutput?(long)w->zstream.total_out:((w->mode == WEB_CLIENT_MODE_FILECOPY)?w->data->rbytes:w->data->bytes);
2894 long size = (w->mode == WEB_CLIENT_MODE_FILECOPY)?w->data->rbytes:w->data->bytes;
2896 if(w->last_url[0]) log_access("%llu: (sent/all = %ld/%ld bytes %0.0f%%, prep/sent/total = %0.2f/%0.2f/%0.2f ms) %s: '%s'",
2898 sent, size, -((size>0)?((float)(size-sent)/(float)size * 100.0):0.0),
2899 (float)usecdiff(&w->tv_ready, &w->tv_in) / 1000.0,
2900 (float)usecdiff(&tv, &w->tv_ready) / 1000.0,
2901 (float)usecdiff(&tv, &w->tv_in) / 1000.0,
2902 (w->mode == WEB_CLIENT_MODE_FILECOPY)?"filecopy":"data",
2906 debug(D_WEB_CLIENT, "%llu: Reseting client.", w->id);
2908 if(w->mode == WEB_CLIENT_MODE_FILECOPY) {
2909 debug(D_WEB_CLIENT, "%llu: Closing filecopy input file.", w->id);
2914 w->last_url[0] = '\0';
2916 w->data->contenttype = CT_TEXT_PLAIN;
2917 w->mode = WEB_CLIENT_MODE_NORMAL;
2919 w->data->rbytes = 0;
2923 w->response_header[0] = '\0';
2924 w->data->buffer[0] = '\0';
2926 w->wait_receive = 1;
2929 // if we had enabled compression, release it
2930 if(w->zinitialized) {
2931 debug(D_DEFLATE, "%llu: Reseting compression.", w->id);
2932 deflateEnd(&w->zstream);
2936 w->zstream.avail_in = 0;
2937 w->zstream.avail_out = 0;
2938 w->zstream.total_in = 0;
2939 w->zstream.total_out = 0;
2940 w->zinitialized = 0;
2944 void web_client_enable_deflate(struct web_client *w) {
2945 if(w->zinitialized == 1) {
2946 error("%llu: Compression has already be initialized for this client.", w->id);
2951 error("%llu: Cannot enable compression in the middle of a conversation.", w->id);
2955 w->zstream.zalloc = Z_NULL;
2956 w->zstream.zfree = Z_NULL;
2957 w->zstream.opaque = Z_NULL;
2959 w->zstream.next_in = (Bytef *)w->data->buffer;
2960 w->zstream.avail_in = 0;
2961 w->zstream.total_in = 0;
2963 w->zstream.next_out = w->zbuffer;
2964 w->zstream.avail_out = 0;
2965 w->zstream.total_out = 0;
2967 w->zstream.zalloc = Z_NULL;
2968 w->zstream.zfree = Z_NULL;
2969 w->zstream.opaque = Z_NULL;
2971 // if(deflateInit(&w->zstream, Z_DEFAULT_COMPRESSION) != Z_OK) {
2972 // error("%llu: Failed to initialize zlib. Proceeding without compression.", w->id);
2976 // Select GZIP compression: windowbits = 15 + 16 = 31
2977 if(deflateInit2(&w->zstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
2978 error("%llu: Failed to initialize zlib. Proceeding without compression.", w->id);
2984 w->zinitialized = 1;
2986 debug(D_DEFLATE, "%llu: Initialized compression.", w->id);
2989 int web_client_data_request(struct web_client *w, char *url, int datasource_type)
2991 char *args = strchr(url, '?');
2997 // get the name of the data to show
2998 char *tok = mystrsep(&url, "/");
2999 debug(D_WEB_CLIENT, "%llu: Searching for RRD data with name '%s'.", w->id, tok);
3001 // do we have such a data set?
3002 RRD_STATS *st = rrd_stats_find_byname(tok);
3003 if(!st) st = rrd_stats_find(tok);
3006 // try to send a file with that name
3008 return(mysendfile(w, tok));
3012 debug(D_WEB_CLIENT, "%llu: Found RRD data with name '%s'.", w->id, tok);
3014 // how many entries does the client want?
3015 long lines = save_history;
3016 long group_count = 1;
3017 time_t after = 0, before = 0;
3018 int group_method = GROUP_AVERAGE;
3022 // parse the lines required
3023 tok = mystrsep(&url, "/");
3024 if(tok) lines = atoi(tok);
3025 if(lines < 1) lines = 1;
3028 // parse the group count required
3029 tok = mystrsep(&url, "/");
3030 if(tok) group_count = atoi(tok);
3031 if(group_count < 1) group_count = 1;
3032 //if(group_count > save_history / 20) group_count = save_history / 20;
3035 // parse the grouping method required
3036 tok = mystrsep(&url, "/");
3037 if(strcmp(tok, "max") == 0) group_method = GROUP_MAX;
3038 else if(strcmp(tok, "average") == 0) group_method = GROUP_AVERAGE;
3039 else debug(D_WEB_CLIENT, "%llu: Unknown group method '%s'", w->id, tok);
3043 tok = mystrsep(&url, "/");
3044 if(tok) after = strtoul(tok, NULL, 10);
3045 if(after < 0) after = 0;
3048 // parse before time
3049 tok = mystrsep(&url, "/");
3050 if(tok) before = strtoul(tok, NULL, 10);
3051 if(before < 0) before = 0;
3055 tok = mystrsep(&url, "/");
3056 if(tok && strcmp(tok, "nonzero") == 0) nonzero = 1;
3059 w->data->contenttype = CT_APPLICATION_JSON;
3062 char *google_version = "0.6";
3063 char *google_reqId = "0";
3064 char *google_sig = "0";
3065 char *google_out = "json";
3066 char *google_responseHandler = "google.visualization.Query.setResponse";
3067 char *google_outFileName = NULL;
3068 unsigned long last_timestamp_in_data = 0;
3069 if(datasource_type == DATASOURCE_GOOGLE_JSON || datasource_type == DATASOURCE_GOOGLE_JSONP) {
3071 w->data->contenttype = CT_APPLICATION_X_JAVASCRIPT;
3074 tok = mystrsep(&args, "&");
3076 char *name = mystrsep(&tok, "=");
3077 if(name && strcmp(name, "tqx") == 0) {
3078 char *key = mystrsep(&tok, ":");
3079 char *value = mystrsep(&tok, ";");
3080 if(key && value && *key && *value) {
3081 if(strcmp(key, "version") == 0)
3082 google_version = value;
3084 else if(strcmp(key, "reqId") == 0)
3085 google_reqId = value;
3087 else if(strcmp(key, "sig") == 0)
3090 else if(strcmp(key, "out") == 0)
3093 else if(strcmp(key, "responseHandler") == 0)
3094 google_responseHandler = value;
3096 else if(strcmp(key, "outFileName") == 0)
3097 google_outFileName = value;
3103 debug(D_WEB_CLIENT_ACCESS, "%llu: GOOGLE JSONP: version = '%s', reqId = '%s', sig = '%s', out = '%s', responseHandler = '%s', outFileName = '%s'",
3104 w->id, google_version, google_reqId, google_sig, google_out, google_responseHandler, google_outFileName
3107 if(datasource_type == DATASOURCE_GOOGLE_JSONP) {
3108 last_timestamp_in_data = strtoul(google_sig, NULL, 0);
3110 // check the client wants json
3111 if(strcmp(google_out, "json") != 0) {
3112 w->data->bytes = snprintf(w->data->buffer, w->data->size,
3113 "%s({version:'%s',reqId:'%s',status:'error',errors:[{reason:'invalid_query',message:'output format is not supported',detailed_message:'the format %s requested is not supported by netdata.'}]});",
3114 google_responseHandler, google_version, google_reqId, google_out);
3120 if(datasource_type == DATASOURCE_GOOGLE_JSONP) {
3121 w->data->bytes = snprintf(w->data->buffer, w->data->size,
3122 "%s({version:'%s',reqId:'%s',status:'ok',sig:'%lu',table:",
3123 google_responseHandler, google_version, google_reqId, st->last_updated.tv_sec);
3126 debug(D_WEB_CLIENT_ACCESS, "%llu: Sending RRD data '%s' (id %s, %d lines, %d group, %d group_method, %lu after, %lu before).", w->id, st->name, st->id, lines, group_count, group_method, after, before);
3127 unsigned long timestamp_in_data = rrd_stats_json(datasource_type, st, w->data, lines, group_count, group_method, after, before, nonzero);
3129 if(datasource_type == DATASOURCE_GOOGLE_JSONP) {
3130 if(timestamp_in_data > last_timestamp_in_data)
3131 w->data->bytes += snprintf(&w->data->buffer[w->data->bytes], w->data->size - w->data->bytes, "});");
3134 // the client already has the latest data
3135 w->data->bytes = snprintf(w->data->buffer, w->data->size,
3136 "%s({version:'%s',reqId:'%s',status:'error',errors:[{reason:'not_modified',message:'Data not modified'}]});",
3137 google_responseHandler, google_version, google_reqId);
3144 void web_client_process(struct web_client *w)
3149 w->wait_receive = 0;
3151 // check if we have an empty line (end of HTTP header)
3152 if(strstr(w->data->buffer, "\r\n\r\n")) {
3153 global_statistics_lock();
3154 global_statistics.web_requests++;
3155 global_statistics_unlock();
3157 gettimeofday(&w->tv_in, NULL);
3158 debug(D_WEB_DATA, "%llu: Processing data buffer of %d bytes: '%s'.", w->id, w->data->bytes, w->data->buffer);
3160 // check if the client requested keep-alive HTTP
3161 if(strcasestr(w->data->buffer, "Connection: keep-alive")) w->keepalive = 1;
3162 else w->keepalive = 0;
3164 // check if the client accepts deflate
3165 if(strstr(w->data->buffer, "gzip"))
3166 web_client_enable_deflate(w);
3168 int datasource_type = DATASOURCE_GOOGLE_JSONP;
3169 //if(strstr(w->data->buffer, "X-DataSource-Auth"))
3170 // datasource_type = DATASOURCE_GOOGLE_JSON;
3172 char *buf = w->data->buffer;
3173 char *tok = strsep(&buf, " \r\n");
3175 char *pointer_to_free = NULL; // keep url_decode() allocated buffer
3177 if(buf && strcmp(tok, "GET") == 0) {
3178 tok = strsep(&buf, " \r\n");
3179 pointer_to_free = url = url_decode(tok);
3180 debug(D_WEB_CLIENT, "%llu: Processing HTTP GET on url '%s'.", w->id, url);
3182 else if (buf && strcmp(tok, "POST") == 0) {
3184 tok = strsep(&buf, " \r\n");
3185 pointer_to_free = url = url_decode(tok);
3187 debug(D_WEB_CLIENT, "%llu: I don't know how to handle POST with form data. Assuming it is a GET on url '%s'.", w->id, url);
3190 w->last_url[0] = '\0';
3192 strncpy(w->last_url, url, URL_MAX);
3193 w->last_url[URL_MAX] = '\0';
3195 tok = mystrsep(&url, "/?&");
3197 debug(D_WEB_CLIENT, "%llu: Processing command '%s'.", w->id, tok);
3199 if(strcmp(tok, WEB_PATH_DATA) == 0) { // "data"
3200 // the client is requesting rrd data
3201 datasource_type = DATASOURCE_JSON;
3202 code = web_client_data_request(w, url, datasource_type);
3204 else if(strcmp(tok, WEB_PATH_DATASOURCE) == 0) { // "datasource"
3205 // the client is requesting google datasource
3206 code = web_client_data_request(w, url, datasource_type);
3208 else if(strcmp(tok, WEB_PATH_GRAPH) == 0) { // "graph"
3209 // the client is requesting an rrd graph
3211 // get the name of the data to show
3212 tok = mystrsep(&url, "/?&");
3213 debug(D_WEB_CLIENT, "%llu: Searching for RRD data with name '%s'.", w->id, tok);
3215 // do we have such a data set?
3216 RRD_STATS *st = rrd_stats_find_byname(tok);
3219 // try to send a file with that name
3221 code = mysendfile(w, tok);
3225 debug(D_WEB_CLIENT_ACCESS, "%llu: Sending %s.json of RRD_STATS...", w->id, st->name);
3226 w->data->contenttype = CT_APPLICATION_JSON;
3228 rrd_stats_graph_json(st, url, w->data);
3231 else if(strcmp(tok, "debug") == 0) {
3234 // get the name of the data to show
3235 tok = mystrsep(&url, "/?&");
3236 debug(D_WEB_CLIENT, "%llu: Searching for RRD data with name '%s'.", w->id, tok);
3238 // do we have such a data set?
3239 RRD_STATS *st = rrd_stats_find_byname(tok);
3242 web_buffer_printf(w->data, "Chart %s is not found.\r\n", tok);
3243 debug(D_WEB_CLIENT_ACCESS, "%llu: %s is not found.", w->id, tok);
3247 debug_flags |= D_RRD_STATS;
3248 st->debug = st->debug?0:1;
3249 web_buffer_printf(w->data, "Chart %s has now debug %s.\r\n", tok, st->debug?"enabled":"disabled");
3250 debug(D_WEB_CLIENT_ACCESS, "%llu: debug for %s is %s.", w->id, tok, st->debug?"enabled":"disabled");
3253 else if(strcmp(tok, "mirror") == 0) {
3256 debug(D_WEB_CLIENT_ACCESS, "%llu: Mirroring...", w->id);
3258 // replace the zero bytes with spaces
3260 for(i = 0; i < w->data->size; i++)
3261 if(w->data->buffer[i] == '\0') w->data->buffer[i] = ' ';
3263 // just leave the buffer as is
3264 // it will be copied back to the client
3266 else if(strcmp(tok, "list") == 0) {
3269 debug(D_WEB_CLIENT_ACCESS, "%llu: Sending list of RRD_STATS...", w->id);
3272 RRD_STATS *st = root;
3274 for ( ; st ; st = st->next )
3275 web_buffer_printf(w->data, "%s\n", st->name);
3277 else if(strcmp(tok, "all.json") == 0) {
3279 debug(D_WEB_CLIENT_ACCESS, "%llu: Sending JSON list of all monitors of RRD_STATS...", w->id);
3281 w->data->contenttype = CT_APPLICATION_JSON;
3283 rrd_stats_all_json(w->data);
3285 else if(strcmp(tok, "netdata.conf") == 0) {
3287 debug(D_WEB_CLIENT_ACCESS, "%llu: Sending netdata.conf ...", w->id);
3289 w->data->contenttype = CT_TEXT_PLAIN;
3291 generate_config(w->data, 0);
3293 else if(strcmp(tok, WEB_PATH_FILE) == 0) { // "file"
3294 tok = mystrsep(&url, "/?&");
3295 if(tok && *tok) code = mysendfile(w, tok);
3299 strcpy(w->data->buffer, "You have to give a filename to get.\r\n");
3300 w->data->bytes = strlen(w->data->buffer);
3305 code = mysendfile(w, "index.html");
3309 code = mysendfile(w, tok);
3314 strcpy(w->last_url, "not a valid response");
3316 if(buf) debug(D_WEB_CLIENT_ACCESS, "%llu: Cannot understand '%s'.", w->id, buf);
3320 strcpy(w->data->buffer, "I don't understand you...\r\n");
3321 w->data->bytes = strlen(w->data->buffer);
3324 // free url_decode() buffer
3325 if(pointer_to_free) free(pointer_to_free);
3327 else if(w->data->bytes > 8192) {
3328 strcpy(w->last_url, "too big request");
3330 debug(D_WEB_CLIENT_ACCESS, "%llu: Received request is too big.", w->id);
3334 strcpy(w->data->buffer, "Received request is too big.\r\n");
3335 w->data->bytes = strlen(w->data->buffer);
3338 // wait for more data
3339 w->wait_receive = 1;
3343 if(w->data->bytes > w->data->size) {
3344 error("%llu: memory overflow encountered (size is %ld, written %ld).", w->data->size, w->data->bytes);
3347 gettimeofday(&w->tv_ready, NULL);
3348 w->data->date = time(NULL);
3351 // prepare the HTTP response header
3352 debug(D_WEB_CLIENT, "%llu: Generating HTTP header with response %d.", w->id, code);
3354 char *content_type_string = "";
3355 switch(w->data->contenttype) {
3357 content_type_string = "text/html";
3360 case CT_APPLICATION_XML:
3361 content_type_string = "application/xml";
3364 case CT_APPLICATION_JSON:
3365 content_type_string = "application/json";
3368 case CT_APPLICATION_X_JAVASCRIPT:
3369 content_type_string = "application/x-javascript";
3373 content_type_string = "text/css";
3377 content_type_string = "text/xml";
3381 content_type_string = "text/xsl";
3384 case CT_APPLICATION_OCTET_STREAM:
3385 content_type_string = "application/octet-stream";
3388 case CT_IMAGE_SVG_XML:
3389 content_type_string = "image/svg+xml";
3392 case CT_APPLICATION_X_FONT_TRUETYPE:
3393 content_type_string = "application/x-font-truetype";
3396 case CT_APPLICATION_X_FONT_OPENTYPE:
3397 content_type_string = "application/x-font-opentype";
3400 case CT_APPLICATION_FONT_WOFF:
3401 content_type_string = "application/font-woff";
3404 case CT_APPLICATION_VND_MS_FONTOBJ:
3405 content_type_string = "application/vnd.ms-fontobject";
3410 content_type_string = "text/plain";
3414 char *code_msg = "";
3421 code_msg = "Temporary Redirect";
3425 code_msg = "Bad Request";
3429 code_msg = "Forbidden";
3433 code_msg = "Not Found";
3437 code_msg = "Internal Server Error";
3442 struct tm tm = *gmtime(&w->data->date);
3443 strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %Z", &tm);
3445 char custom_header[MAX_HTTP_HEADER_SIZE + 1] = "";
3446 if(w->response_header[0])
3447 strcpy(custom_header, w->response_header);
3450 headerlen += snprintf(&w->response_header[headerlen], MAX_HTTP_HEADER_SIZE - headerlen,
3451 "HTTP/1.1 %d %s\r\n"
3452 "Connection: %s\r\n"
3453 "Server: NetData Embedded HTTP Server\r\n"
3454 "Content-Type: %s\r\n"
3455 "Access-Control-Allow-Origin: *\r\n"
3458 , w->keepalive?"keep-alive":"close"
3459 , content_type_string
3463 if(custom_header[0])
3464 headerlen += snprintf(&w->response_header[headerlen], MAX_HTTP_HEADER_SIZE - headerlen, "%s", custom_header);
3466 if(w->mode == WEB_CLIENT_MODE_NORMAL) {
3467 headerlen += snprintf(&w->response_header[headerlen], MAX_HTTP_HEADER_SIZE - headerlen,
3469 "Cache-Control: no-cache\r\n"
3474 headerlen += snprintf(&w->response_header[headerlen], MAX_HTTP_HEADER_SIZE - headerlen,
3475 "Cache-Control: public\r\n"
3479 // if we know the content length, put it
3480 if(!w->zoutput && (w->data->bytes || w->data->rbytes))
3481 headerlen += snprintf(&w->response_header[headerlen], MAX_HTTP_HEADER_SIZE - headerlen,
3482 "Content-Length: %ld\r\n"
3483 , w->data->bytes?w->data->bytes:w->data->rbytes
3485 else if(!w->zoutput)
3486 w->keepalive = 0; // content-length is required for keep-alive
3489 headerlen += snprintf(&w->response_header[headerlen], MAX_HTTP_HEADER_SIZE - headerlen,
3490 "Content-Encoding: gzip\r\n"
3491 "Transfer-Encoding: chunked\r\n"
3495 headerlen += snprintf(&w->response_header[headerlen], MAX_HTTP_HEADER_SIZE - headerlen, "\r\n");
3497 // disable TCP_NODELAY, to buffer the header
3499 if(setsockopt(w->ofd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) != 0) error("%llu: failed to disable TCP_NODELAY on socket.", w->id);
3501 // sent the HTTP header
3502 debug(D_WEB_DATA, "%llu: Sending response HTTP header of size %d: '%s'", w->id, headerlen, w->response_header);
3504 bytes = send(w->ofd, w->response_header, headerlen, 0);
3505 if(bytes != headerlen)
3506 error("%llu: HTTP Header failed to be sent (I sent %d bytes but the system sent %d bytes).", w->id, headerlen, bytes);
3508 global_statistics_lock();
3509 global_statistics.bytes_sent += bytes;
3510 global_statistics_unlock();
3513 // enable TCP_NODELAY, to send all data immediately at the next send()
3515 if(setsockopt(w->ofd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) != 0) error("%llu: failed to enable TCP_NODELAY on socket.", w->id);
3517 // enable sending immediately if we have data
3518 if(w->data->bytes) w->wait_send = 1;
3519 else w->wait_send = 0;
3523 case WEB_CLIENT_MODE_NORMAL:
3524 debug(D_WEB_CLIENT, "%llu: Done preparing the response. Sending data (%d bytes) to client.", w->id, w->data->bytes);
3527 case WEB_CLIENT_MODE_FILECOPY:
3528 if(w->data->rbytes) {
3529 debug(D_WEB_CLIENT, "%llu: Done preparing the response. Will be sending data file of %d bytes to client.", w->id, w->data->rbytes);
3530 w->wait_receive = 1;
3533 // utilize the kernel sendfile() for copying the file to the socket.
3534 // this block of code can be commented, without anything missing.
3535 // when it is commented, the program will copy the data using async I/O.
3537 long len = sendfile(w->ofd, w->ifd, NULL, w->data->rbytes);
3538 if(len != w->data->rbytes) error("%llu: sendfile() should copy %ld bytes, but copied %ld. Falling back to manual copy.", w->id, w->data->rbytes, len);
3539 else web_client_reset(w);
3544 debug(D_WEB_CLIENT, "%llu: Done preparing the response. Will be sending an unknown amount of bytes to client.", w->id);
3548 fatal("%llu: Unknown client mode %d.", w->id, w->mode);
3553 long web_client_send_chunk_header(struct web_client *w, int len)
3555 debug(D_DEFLATE, "%llu: OPEN CHUNK of %d bytes (hex: %x).", w->id, len, len);
3557 sprintf(buf, "%X\r\n", len);
3558 int bytes = send(w->ofd, buf, strlen(buf), MSG_DONTWAIT);
3560 if(bytes > 0) debug(D_DEFLATE, "%llu: Sent chunk header %d bytes.", w->id, bytes);
3561 else if(bytes == 0) debug(D_DEFLATE, "%llu: Did not send chunk header to the client.", w->id);
3562 else debug(D_DEFLATE, "%llu: Failed to send chunk header to client. Reason: %s", w->id, strerror(errno));
3567 long web_client_send_chunk_close(struct web_client *w)
3569 //debug(D_DEFLATE, "%llu: CLOSE CHUNK.", w->id);
3571 int bytes = send(w->ofd, "\r\n", 2, MSG_DONTWAIT);
3573 if(bytes > 0) debug(D_DEFLATE, "%llu: Sent chunk suffix %d bytes.", w->id, bytes);
3574 else if(bytes == 0) debug(D_DEFLATE, "%llu: Did not send chunk suffix to the client.", w->id);
3575 else debug(D_DEFLATE, "%llu: Failed to send chunk suffix to client. Reason: %s", w->id, strerror(errno));
3580 long web_client_send_chunk_finalize(struct web_client *w)
3582 //debug(D_DEFLATE, "%llu: FINALIZE CHUNK.", w->id);
3584 int bytes = send(w->ofd, "\r\n0\r\n\r\n", 7, MSG_DONTWAIT);
3586 if(bytes > 0) debug(D_DEFLATE, "%llu: Sent chunk suffix %d bytes.", w->id, bytes);
3587 else if(bytes == 0) debug(D_DEFLATE, "%llu: Did not send chunk suffix to the client.", w->id);
3588 else debug(D_DEFLATE, "%llu: Failed to send chunk suffix to client. Reason: %s", w->id, strerror(errno));
3593 long web_client_send_deflate(struct web_client *w)
3595 long bytes = 0, t = 0;
3597 // when using compression,
3598 // w->data->sent is the amount of bytes passed through compression
3600 // debug(D_DEFLATE, "%llu: TEST w->data->bytes = %d, w->data->sent = %d, w->zhave = %d, w->zsent = %d, w->zstream.avail_in = %d, w->zstream.avail_out = %d, w->zstream.total_in = %d, w->zstream.total_out = %d.", w->id, w->data->bytes, w->data->sent, w->zhave, w->zsent, w->zstream.avail_in, w->zstream.avail_out, w->zstream.total_in, w->zstream.total_out);
3602 if(w->data->bytes - w->data->sent == 0 && w->zstream.avail_in == 0 && w->zhave == w->zsent && w->zstream.avail_out != 0) {
3603 // there is nothing to send
3605 debug(D_WEB_CLIENT, "%llu: Out of output data.", w->id);
3607 // finalize the chunk
3608 if(w->data->sent != 0)
3609 t += web_client_send_chunk_finalize(w);
3611 // there can be two cases for this
3612 // A. we have done everything
3613 // B. we temporarily have nothing to send, waiting for the buffer to be filled by ifd
3615 if(w->mode == WEB_CLIENT_MODE_FILECOPY && w->wait_receive && w->ifd != w->ofd && w->data->rbytes && w->data->rbytes > w->data->bytes) {
3616 // we have to wait, more data will come
3617 debug(D_WEB_CLIENT, "%llu: Waiting for more data to become available.", w->id);
3622 if(w->keepalive == 0) {
3623 debug(D_WEB_CLIENT, "%llu: Closing (keep-alive is not enabled). %ld bytes sent.", w->id, w->data->sent);
3629 web_client_reset(w);
3630 debug(D_WEB_CLIENT, "%llu: Done sending all data on socket. Waiting for next request on the same socket.", w->id);
3634 if(w->zstream.avail_out == 0 && w->zhave == w->zsent) {
3635 // compress more input data
3637 // close the previous open chunk
3638 if(w->data->sent != 0) t += web_client_send_chunk_close(w);
3640 debug(D_DEFLATE, "%llu: Compressing %d bytes starting from %d.", w->id, (w->data->bytes - w->data->sent), w->data->sent);
3642 // give the compressor all the data not passed through the compressor yet
3643 if(w->data->bytes > w->data->sent) {
3644 w->zstream.next_in = (Bytef *)&w->data->buffer[w->data->sent];
3645 w->zstream.avail_in = (w->data->bytes - w->data->sent);
3648 // reset the compressor output buffer
3649 w->zstream.next_out = w->zbuffer;
3650 w->zstream.avail_out = ZLIB_CHUNK;
3652 // ask for FINISH if we have all the input
3653 int flush = Z_SYNC_FLUSH;
3654 if(w->mode == WEB_CLIENT_MODE_NORMAL
3655 || (w->mode == WEB_CLIENT_MODE_FILECOPY && w->data->bytes == w->data->rbytes)) {
3657 debug(D_DEFLATE, "%llu: Requesting Z_FINISH.", w->id);
3660 debug(D_DEFLATE, "%llu: Requesting Z_SYNC_FLUSH.", w->id);
3664 if(deflate(&w->zstream, flush) == Z_STREAM_ERROR) {
3665 error("%llu: Compression failed. Closing down client.", w->id);
3666 web_client_reset(w);
3670 w->zhave = ZLIB_CHUNK - w->zstream.avail_out;
3673 // keep track of the bytes passed through the compressor
3674 w->data->sent = w->data->bytes;
3676 debug(D_DEFLATE, "%llu: Compression produced %d bytes.", w->id, w->zhave);
3679 t += web_client_send_chunk_header(w, w->zhave);
3682 bytes = send(w->ofd, &w->zbuffer[w->zsent], w->zhave - w->zsent, MSG_DONTWAIT);
3685 if(t > 0) bytes += t;
3686 debug(D_WEB_CLIENT, "%llu: Sent %d bytes.", w->id, bytes);
3688 else if(bytes == 0) debug(D_WEB_CLIENT, "%llu: Did not send any bytes to the client.", w->id);
3689 else debug(D_WEB_CLIENT, "%llu: Failed to send data to client. Reason: %s", w->id, strerror(errno));
3694 long web_client_send(struct web_client *w)
3696 if(w->zoutput) return web_client_send_deflate(w);
3700 if(w->data->bytes - w->data->sent == 0) {
3701 // there is nothing to send
3703 debug(D_WEB_CLIENT, "%llu: Out of output data.", w->id);
3705 // there can be two cases for this
3706 // A. we have done everything
3707 // B. we temporarily have nothing to send, waiting for the buffer to be filled by ifd
3709 if(w->mode == WEB_CLIENT_MODE_FILECOPY && w->wait_receive && w->ifd != w->ofd && w->data->rbytes && w->data->rbytes > w->data->bytes) {
3710 // we have to wait, more data will come
3711 debug(D_WEB_CLIENT, "%llu: Waiting for more data to become available.", w->id);
3716 if(w->keepalive == 0) {
3717 debug(D_WEB_CLIENT, "%llu: Closing (keep-alive is not enabled). %ld bytes sent.", w->id, w->data->sent);
3722 web_client_reset(w);
3723 debug(D_WEB_CLIENT, "%llu: Done sending all data on socket. Waiting for next request on the same socket.", w->id);
3727 bytes = send(w->ofd, &w->data->buffer[w->data->sent], w->data->bytes - w->data->sent, MSG_DONTWAIT);
3729 w->data->sent += bytes;
3730 debug(D_WEB_CLIENT, "%llu: Sent %d bytes.", w->id, bytes);
3732 else if(bytes == 0) debug(D_WEB_CLIENT, "%llu: Did not send any bytes to the client.", w->id);
3733 else debug(D_WEB_CLIENT, "%llu: Failed to send data to client. Reason: %s", w->id, strerror(errno));
3739 long web_client_receive(struct web_client *w)
3741 // do we have any space for more data?
3742 web_buffer_increase(w->data, WEB_DATA_LENGTH_INCREASE_STEP);
3744 long left = w->data->size - w->data->bytes;
3747 if(w->mode == WEB_CLIENT_MODE_FILECOPY)
3748 bytes = read(w->ifd, &w->data->buffer[w->data->bytes], (left-1));
3750 bytes = recv(w->ifd, &w->data->buffer[w->data->bytes], left-1, MSG_DONTWAIT);
3753 int old = w->data->bytes;
3754 w->data->bytes += bytes;
3755 w->data->buffer[w->data->bytes] = '\0';
3757 debug(D_WEB_CLIENT, "%llu: Received %d bytes.", w->id, bytes);
3758 debug(D_WEB_DATA, "%llu: Received data: '%s'.", w->id, &w->data->buffer[old]);
3760 if(w->mode == WEB_CLIENT_MODE_FILECOPY) {
3762 if(w->data->rbytes && w->data->bytes >= w->data->rbytes) w->wait_receive = 0;
3765 else if(bytes == 0) {
3766 debug(D_WEB_CLIENT, "%llu: Out of input data.", w->id);
3768 // if we cannot read, it means we have an error on input.
3769 // if however, we are copying a file from ifd to ofd, we should not return an error.
3770 // in this case, the error should be generated when the file has been sent to the client.
3772 if(w->mode == WEB_CLIENT_MODE_FILECOPY) {
3773 // we are copying data fron ifd to ofd
3774 // let it finish copying...
3775 w->wait_receive = 0;
3776 debug(D_WEB_CLIENT, "%llu: Disabling input.", w->id);
3788 // --------------------------------------------------------------------------------------
3789 // the thread of a single client
3791 // 1. waits for input and output, using async I/O
3792 // 2. it processes HTTP requests
3793 // 3. it generates HTTP responses
3794 // 4. it copies data from input to output if mode is FILECOPY
3796 void *new_client(void *ptr)
3798 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
3799 error("Cannot set pthread cancel type to DEFERRED.");
3801 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
3802 error("Cannot set pthread cancel state to ENABLE.");
3805 struct web_client *w = ptr;
3807 fd_set ifds, ofds, efds;
3815 FD_SET(w->ifd, &efds);
3816 if(w->ifd != w->ofd) FD_SET(w->ofd, &efds);
3817 if (w->wait_receive) {
3818 FD_SET(w->ifd, &ifds);
3819 if(w->ifd > fdmax) fdmax = w->ifd;
3822 FD_SET(w->ofd, &ofds);
3823 if(w->ofd > fdmax) fdmax = w->ofd;
3829 debug(D_WEB_CLIENT, "%llu: Waiting socket async I/O for %s %s", w->id, w->wait_receive?"INPUT":"", w->wait_send?"OUTPUT":"");
3830 retval = select(fdmax+1, &ifds, &ofds, &efds, &tv);
3833 error("%llu: LISTENER: select() failed.", w->id);
3838 web_client_reset(w);
3843 if(FD_ISSET(w->ifd, &efds)) {
3844 debug(D_WEB_CLIENT_ACCESS, "%llu: Received error on input socket (%s).", w->id, strerror(errno));
3845 web_client_reset(w);
3850 if(FD_ISSET(w->ofd, &efds)) {
3851 debug(D_WEB_CLIENT_ACCESS, "%llu: Received error on output socket (%s).", w->id, strerror(errno));
3852 web_client_reset(w);
3857 if(w->wait_send && FD_ISSET(w->ofd, &ofds)) {
3859 if((bytes = web_client_send(w)) < 0) {
3860 debug(D_WEB_CLIENT, "%llu: Closing client (input: %s).", w->id, strerror(errno));
3861 web_client_reset(w);
3867 global_statistics_lock();
3868 global_statistics.bytes_sent += bytes;
3869 global_statistics_unlock();
3873 if(w->wait_receive && FD_ISSET(w->ifd, &ifds)) {
3875 if((bytes = web_client_receive(w)) < 0) {
3876 debug(D_WEB_CLIENT, "%llu: Closing client (output: %s).", w->id, strerror(errno));
3877 web_client_reset(w);
3883 if(w->mode != WEB_CLIENT_MODE_FILECOPY) {
3884 global_statistics_lock();
3885 global_statistics.bytes_received += bytes;
3886 global_statistics_unlock();
3890 if(w->mode == WEB_CLIENT_MODE_NORMAL) web_client_process(w);
3893 debug(D_WEB_CLIENT, "%llu: done...", w->id);
3898 // --------------------------------------------------------------------------------------
3899 // the main socket listener
3901 // 1. it accepts new incoming requests on our port
3902 // 2. creates a new web_client for each connection received
3903 // 3. spawns a new pthread to serve the client (this is optimal for keep-alive clients)
3904 // 4. cleans up old web_clients that their pthreads have been exited
3906 void log_allocations(void)
3913 if(mi.uordblks > mem) {
3915 struct web_client *w;
3916 for(w = web_clients; w ; w = w->next) clients++;
3918 info("Allocated memory increased from %d to %d (increased by %d bytes). There are %d web clients connected.", mem, mi.uordblks, mi.uordblks - mem, clients);
3923 void *socket_listen_main(void *ptr)
3926 struct web_client *w;
3930 // int listener = create_listen_socket(listen_port);
3931 int listener = listen_fd;
3932 if(listener == -1) fatal("LISTENER: Cannot create listening socket on port 19999.");
3934 fd_set ifds, ofds, efds;
3935 int fdmax = listener;
3943 tv.tv_usec = 200000;
3945 FD_SET(listener, &ifds);
3946 FD_SET(listener, &efds);
3948 // debug(D_WEB_CLIENT, "LISTENER: Waiting...");
3949 retval = select(fdmax+1, &ifds, &ofds, &efds, &tv);
3952 error("LISTENER: select() failed.");
3956 // check for new incoming connections
3957 if(FD_ISSET(listener, &ifds)) {
3958 w = web_client_create(listener);
3960 if(pthread_create(&w->thread, NULL, new_client, w) != 0) {
3961 error("%llu: failed to create new thread for web client.");
3964 else if(pthread_detach(w->thread) != 0) {
3965 error("%llu: Cannot request detach of newly created web client thread.", w->id);
3969 log_access("%llu: %s connected", w->id, w->client_ip);
3971 else debug(D_WEB_CLIENT, "LISTENER: select() didn't do anything.");
3974 //else debug(D_WEB_CLIENT, "LISTENER: select() timeout.");
3976 // cleanup unused clients
3977 for(w = web_clients; w ; w = w?w->next:NULL) {
3979 log_access("%llu: %s disconnected", w->id, w->client_ip);
3980 debug(D_WEB_CLIENT, "%llu: Removing client.", w->id);
3981 // pthread_join(w->thread, NULL);
3982 w = web_client_free(w);
3988 error("LISTENER: exit!");
3996 // ----------------------------------------------------------------------------
3997 // /proc/net/dev processor
3999 #define MAX_PROC_NET_DEV_LINE 4096
4000 #define MAX_PROC_NET_DEV_IFACE_NAME 1024
4002 int do_proc_net_dev() {
4003 static int enable_new_interfaces = -1;
4004 static int do_bandwidth = -1, do_packets = -1, do_errors = -1, do_fifo = -1, do_compressed = -1;
4006 if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean("plugin:proc:/proc/net/dev", "enable new interfaces detected at runtime", 1);
4008 if(do_bandwidth == -1) do_bandwidth = config_get_boolean("plugin:proc:/proc/net/dev", "bandwidth for all interfaces", 1);
4009 if(do_packets == -1) do_packets = config_get_boolean("plugin:proc:/proc/net/dev", "packets for all interfaces", 1);
4010 if(do_errors == -1) do_errors = config_get_boolean("plugin:proc:/proc/net/dev", "errors for all interfaces", 1);
4011 if(do_fifo == -1) do_fifo = config_get_boolean("plugin:proc:/proc/net/dev", "fifo for all interfaces", 1);
4012 if(do_compressed == -1) do_compressed = config_get_boolean("plugin:proc:/proc/net/dev", "compressed packets for all interfaces", 1);
4014 char buffer[MAX_PROC_NET_DEV_LINE+1] = "";
4015 char iface[MAX_PROC_NET_DEV_IFACE_NAME + 1] = "";
4016 unsigned long long rbytes, rpackets, rerrors, rdrops, rfifo, rframe, rcompressed, rmulticast;
4017 unsigned long long tbytes, tpackets, terrors, tdrops, tfifo, tcollisions, tcarrier, tcompressed;
4022 FILE *fp = fopen("/proc/net/dev", "r");
4024 error("Cannot read /proc/net/dev.");
4028 // skip the first two lines
4029 p = fgets(buffer, MAX_PROC_NET_DEV_LINE, fp);
4030 p = fgets(buffer, MAX_PROC_NET_DEV_LINE, fp);
4032 // read the rest of the lines
4035 p = fgets(buffer, MAX_PROC_NET_DEV_LINE, fp);
4038 c = strchr(buffer, ':');
4041 r = sscanf(buffer, "%s %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
4043 &rbytes, &rpackets, &rerrors, &rdrops, &rfifo, &rframe, &rcompressed, &rmulticast,
4044 &tbytes, &tpackets, &terrors, &tdrops, &tfifo, &tcollisions, &tcarrier, &tcompressed);
4047 error("Cannot read /proc/net/dev line. Expected 17 params, read %d.", r);
4051 // check if it is enabled
4053 char var_name[4096 + 1];
4054 snprintf(var_name, 4096, "interface %s", iface);
4055 if(!config_get_boolean("plugin:proc:/proc/net/dev", var_name, enable_new_interfaces)) continue;
4060 // --------------------------------------------------------------------
4063 st = rrd_stats_find_bytype(RRD_TYPE_NET, iface);
4065 st = rrd_stats_create(RRD_TYPE_NET, iface, NULL, iface, "Bandwidth", "kilobits/s", 1000, update_every, CHART_TYPE_AREA);
4067 rrd_stats_dimension_add(st, "received", NULL, 8, 1024, RRD_DIMENSION_INCREMENTAL);
4068 rrd_stats_dimension_add(st, "sent", NULL, -8, 1024, RRD_DIMENSION_INCREMENTAL);
4070 else rrd_stats_next(st);
4072 rrd_stats_dimension_set(st, "received", rbytes);
4073 rrd_stats_dimension_set(st, "sent", tbytes);
4077 // --------------------------------------------------------------------
4080 st = rrd_stats_find_bytype("net_packets", iface);
4082 st = rrd_stats_create("net_packets", iface, NULL, iface, "Packets", "packets/s", 1001, update_every, CHART_TYPE_LINE);
4085 rrd_stats_dimension_add(st, "received", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4086 rrd_stats_dimension_add(st, "sent", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4088 else rrd_stats_next(st);
4090 rrd_stats_dimension_set(st, "received", rpackets);
4091 rrd_stats_dimension_set(st, "sent", tpackets);
4095 // --------------------------------------------------------------------
4098 st = rrd_stats_find_bytype("net_errors", iface);
4100 st = rrd_stats_create("net_errors", iface, NULL, iface, "Interface Errors", "errors/s", 1002, update_every, CHART_TYPE_LINE);
4103 rrd_stats_dimension_add(st, "receive", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4104 rrd_stats_dimension_add(st, "transmit", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4106 else rrd_stats_next(st);
4108 rrd_stats_dimension_set(st, "receive", rerrors);
4109 rrd_stats_dimension_set(st, "transmit", terrors);
4113 // --------------------------------------------------------------------
4116 st = rrd_stats_find_bytype("net_fifo", iface);
4118 st = rrd_stats_create("net_fifo", iface, NULL, iface, "Interface Queue", "packets", 1100, update_every, CHART_TYPE_LINE);
4121 rrd_stats_dimension_add(st, "receive", NULL, 1, 1, RRD_DIMENSION_ABSOLUTE);
4122 rrd_stats_dimension_add(st, "transmit", NULL, -1, 1, RRD_DIMENSION_ABSOLUTE);
4124 else rrd_stats_next(st);
4126 rrd_stats_dimension_set(st, "receive", rfifo);
4127 rrd_stats_dimension_set(st, "transmit", tfifo);
4131 // --------------------------------------------------------------------
4134 st = rrd_stats_find_bytype("net_compressed", iface);
4136 st = rrd_stats_create("net_compressed", iface, NULL, iface, "Compressed Packets", "packets/s", 1200, update_every, CHART_TYPE_LINE);
4139 rrd_stats_dimension_add(st, "received", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4140 rrd_stats_dimension_add(st, "sent", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4142 else rrd_stats_next(st);
4144 rrd_stats_dimension_set(st, "received", rcompressed);
4145 rrd_stats_dimension_set(st, "sent", tcompressed);
4154 // ----------------------------------------------------------------------------
4155 // /proc/diskstats processor
4157 #define MAX_PROC_DISKSTATS_LINE 4096
4158 #define MAX_PROC_DISKSTATS_DISK_NAME 1024
4160 int do_proc_diskstats() {
4161 static int enable_new_disks = -1;
4162 static int do_io = -1, do_ops = -1, do_merged_ops = -1, do_iotime = -1, do_cur_ops = -1;
4164 if(enable_new_disks == -1) enable_new_disks = config_get_boolean("plugin:proc:/proc/diskstats", "enable new disks detected at runtime", 1);
4166 if(do_io == -1) do_io = config_get_boolean("plugin:proc:/proc/diskstats", "bandwidth for all disks", 1);
4167 if(do_ops == -1) do_ops = config_get_boolean("plugin:proc:/proc/diskstats", "operations for all disks", 1);
4168 if(do_merged_ops == -1) do_merged_ops = config_get_boolean("plugin:proc:/proc/diskstats", "merged operations for all disks", 1);
4169 if(do_iotime == -1) do_iotime = config_get_boolean("plugin:proc:/proc/diskstats", "i/o time for all disks", 1);
4170 if(do_cur_ops == -1) do_cur_ops = config_get_boolean("plugin:proc:/proc/diskstats", "current operations for all disks", 1);
4172 char buffer[MAX_PROC_DISKSTATS_LINE+1] = "";
4173 char disk[MAX_PROC_DISKSTATS_DISK_NAME + 1] = "";
4178 FILE *fp = fopen("/proc/diskstats", "r");
4180 error("Cannot read /proc/diskstats.");
4185 unsigned long long major = 0, minor = 0,
4186 reads = 0, reads_merged = 0, readsectors = 0, readms = 0,
4187 writes = 0, writes_merged = 0, writesectors = 0, writems = 0,
4188 currentios = 0, iosms = 0, wiosms = 0;
4190 p = fgets(buffer, MAX_PROC_DISKSTATS_LINE, fp);
4193 r = sscanf(buffer, "%llu %llu %s %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
4194 &major, &minor, disk,
4195 &reads, &reads_merged, &readsectors, &readms, &writes, &writes_merged, &writesectors, &writems, ¤tios, &iosms, &wiosms
4199 error("Cannot read /proc/diskstats line. Expected 14 params, read %d.", r);
4203 int def_enabled = 0;
4207 case 43: // network block
4211 case 199: // veritas
4212 case 201: // veritas
4214 def_enabled = enable_new_disks;
4236 if(minor % 8) def_enabled = 0; // partitions
4237 else def_enabled = enable_new_disks;
4240 case 8: // scsi disks
4241 case 65: // scsi disks
4242 case 66: // scsi disks
4243 case 67: // scsi disks
4244 case 68: // scsi disks
4245 case 69: // scsi disks
4246 case 70: // scsi disks
4247 case 71: // scsi disks
4248 case 72: // scsi disks
4249 case 73: // scsi disks
4250 case 74: // scsi disks
4251 case 75: // scsi disks
4252 case 76: // scsi disks
4253 case 77: // scsi disks
4254 case 78: // scsi disks
4255 case 79: // scsi disks
4264 case 101: // hyperdisk
4265 case 102: // compressed
4274 case 114: // bios raid
4275 case 116: // ram board
4288 if(minor % 16) def_enabled = 0; // partitions
4289 else def_enabled = enable_new_disks;
4294 if(minor % 32) def_enabled = 0; // partitions
4295 else def_enabled = enable_new_disks;
4299 case 13: // 8bit ide
4309 if(minor % 64) def_enabled = 0; // partitions
4310 else def_enabled = enable_new_disks;
4318 // check if it is enabled
4320 char var_name[4096 + 1];
4321 snprintf(var_name, 4096, "disk %s", disk);
4322 if(!config_get_boolean("plugin:proc:/proc/diskstats", var_name, def_enabled)) continue;
4327 // --------------------------------------------------------------------
4330 st = rrd_stats_find_bytype(RRD_TYPE_DISK, disk);
4332 char tf[FILENAME_MAX + 1], *t;
4333 char ssfilename[FILENAME_MAX + 1];
4334 int sector_size = 512;
4336 strncpy(tf, disk, FILENAME_MAX);
4337 tf[FILENAME_MAX] = '\0';
4339 // replace all / with !
4340 while((t = strchr(tf, '/'))) *t = '!';
4342 snprintf(ssfilename, FILENAME_MAX, "/sys/block/%s/queue/hw_sector_size", tf);
4343 FILE *fpss = fopen(ssfilename, "r");
4345 char ssbuffer[1025];
4346 char *tmp = fgets(ssbuffer, 1024, fpss);
4349 sector_size = atoi(tmp);
4350 if(sector_size <= 0) {
4351 error("Invalid sector size %d for device %s in %s. Assuming 512.", sector_size, disk, ssfilename);
4355 else error("Cannot read data for sector size for device %s from %s. Assuming 512.", disk, ssfilename);
4359 else error("Cannot read sector size for device %s from %s. Assuming 512.", disk, ssfilename);
4361 st = rrd_stats_create(RRD_TYPE_DISK, disk, NULL, disk, "Disk I/O", "kilobytes/s", 2000, update_every, CHART_TYPE_AREA);
4363 rrd_stats_dimension_add(st, "reads", NULL, sector_size, 1024, RRD_DIMENSION_INCREMENTAL);
4364 rrd_stats_dimension_add(st, "writes", NULL, sector_size * -1, 1024, RRD_DIMENSION_INCREMENTAL);
4366 else rrd_stats_next(st);
4368 rrd_stats_dimension_set(st, "reads", readsectors);
4369 rrd_stats_dimension_set(st, "writes", writesectors);
4373 // --------------------------------------------------------------------
4376 st = rrd_stats_find_bytype("disk_ops", disk);
4378 st = rrd_stats_create("disk_ops", disk, NULL, disk, "Disk Operations", "operations/s", 2001, update_every, CHART_TYPE_LINE);
4381 rrd_stats_dimension_add(st, "reads", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4382 rrd_stats_dimension_add(st, "writes", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4384 else rrd_stats_next(st);
4386 rrd_stats_dimension_set(st, "reads", reads);
4387 rrd_stats_dimension_set(st, "writes", writes);
4391 // --------------------------------------------------------------------
4394 st = rrd_stats_find_bytype("disk_merged_ops", disk);
4396 st = rrd_stats_create("disk_merged_ops", disk, NULL, disk, "Merged Disk Operations", "operations/s", 2010, update_every, CHART_TYPE_LINE);
4399 rrd_stats_dimension_add(st, "reads", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4400 rrd_stats_dimension_add(st, "writes", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4402 else rrd_stats_next(st);
4404 rrd_stats_dimension_set(st, "reads", reads_merged);
4405 rrd_stats_dimension_set(st, "writes", writes_merged);
4409 // --------------------------------------------------------------------
4412 st = rrd_stats_find_bytype("disk_iotime", disk);
4414 st = rrd_stats_create("disk_iotime", disk, NULL, disk, "Disk I/O Time", "milliseconds/s", 2005, update_every, CHART_TYPE_LINE);
4417 rrd_stats_dimension_add(st, "reads", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4418 rrd_stats_dimension_add(st, "writes", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4419 rrd_stats_dimension_add(st, "latency", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4420 rrd_stats_dimension_add(st, "weighted", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4422 else rrd_stats_next(st);
4424 rrd_stats_dimension_set(st, "reads", readms);
4425 rrd_stats_dimension_set(st, "writes", writems);
4426 rrd_stats_dimension_set(st, "latency", iosms);
4427 rrd_stats_dimension_set(st, "weighted", wiosms);
4431 // --------------------------------------------------------------------
4434 st = rrd_stats_find_bytype("disk_cur_ops", disk);
4436 st = rrd_stats_create("disk_cur_ops", disk, NULL, disk, "Current Disk I/O operations", "operations", 2004, update_every, CHART_TYPE_LINE);
4439 rrd_stats_dimension_add(st, "operations", NULL, 1, 1, RRD_DIMENSION_ABSOLUTE);
4441 else rrd_stats_next(st);
4443 rrd_stats_dimension_set(st, "operations", currentios);
4452 // ----------------------------------------------------------------------------
4453 // /proc/net/snmp processor
4455 int do_proc_net_snmp() {
4456 static int do_ip_packets = -1, do_ip_fragsout = -1, do_ip_fragsin = -1, do_ip_errors = -1,
4457 do_tcp_sockets = -1, do_tcp_packets = -1, do_tcp_errors = -1, do_tcp_handshake = -1,
4458 do_udp_packets = -1, do_udp_errors = -1;
4460 if(do_ip_packets == -1) do_ip_packets = config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 packets", 1);
4461 if(do_ip_fragsout == -1) do_ip_fragsout = config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 fragrments sent", 1);
4462 if(do_ip_fragsin == -1) do_ip_fragsin = config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 fragments assembly", 1);
4463 if(do_ip_errors == -1) do_ip_errors = config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 errors", 1);
4464 if(do_tcp_sockets == -1) do_tcp_sockets = config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 TCP connections", 1);
4465 if(do_tcp_packets == -1) do_tcp_packets = config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 TCP packets", 1);
4466 if(do_tcp_errors == -1) do_tcp_errors = config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 TCP errors", 1);
4467 if(do_tcp_handshake == -1) do_tcp_handshake = config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 TCP handshake issues", 1);
4468 if(do_udp_packets == -1) do_udp_packets = config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 UDP packets", 1);
4469 if(do_udp_errors == -1) do_udp_errors = config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 UDP errors", 1);
4471 char buffer[MAX_PROC_NET_SNMP_LINE+1] = "";
4473 FILE *fp = fopen("/proc/net/snmp", "r");
4475 error("Cannot read /proc/net/snmp.");
4482 char *p = fgets(buffer, MAX_PROC_NET_SNMP_LINE, fp);
4485 if(strncmp(p, "Ip: ", 4) == 0) {
4486 // skip the header line, read the data
4487 p = fgets(buffer, MAX_PROC_NET_SNMP_LINE, fp);
4490 if(strncmp(p, "Ip: ", 4) != 0) {
4491 error("Cannot read IP line from /proc/net/snmp.");
4495 // see also http://net-snmp.sourceforge.net/docs/mibs/ip.html
4496 unsigned long long Forwarding, DefaultTTL, InReceives, InHdrErrors, InAddrErrors, ForwDatagrams, InUnknownProtos, InDiscards, InDelivers,
4497 OutRequests, OutDiscards, OutNoRoutes, ReasmTimeout, ReasmReqds, ReasmOKs, ReasmFails, FragOKs, FragFails, FragCreates;
4499 int r = sscanf(&buffer[4], "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
4500 &Forwarding, &DefaultTTL, &InReceives, &InHdrErrors, &InAddrErrors, &ForwDatagrams, &InUnknownProtos, &InDiscards, &InDelivers,
4501 &OutRequests, &OutDiscards, &OutNoRoutes, &ReasmTimeout, &ReasmReqds, &ReasmOKs, &ReasmFails, &FragOKs, &FragFails, &FragCreates);
4504 if(r != 19) error("Cannot read /proc/net/snmp IP line. Expected 19 params, read %d.", r);
4506 // --------------------------------------------------------------------
4509 st = rrd_stats_find(RRD_TYPE_NET_SNMP ".packets");
4511 st = rrd_stats_create(RRD_TYPE_NET_SNMP, "packets", NULL, RRD_TYPE_NET_SNMP, "IPv4 Packets", "packets/s", 3000, update_every, CHART_TYPE_LINE);
4513 rrd_stats_dimension_add(st, "received", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4514 rrd_stats_dimension_add(st, "sent", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4515 rrd_stats_dimension_add(st, "forwarded", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4517 else rrd_stats_next(st);
4519 rrd_stats_dimension_set(st, "sent", OutRequests);
4520 rrd_stats_dimension_set(st, "received", InReceives);
4521 rrd_stats_dimension_set(st, "forwarded", ForwDatagrams);
4525 // --------------------------------------------------------------------
4527 if(do_ip_fragsout) {
4528 st = rrd_stats_find(RRD_TYPE_NET_SNMP ".fragsout");
4530 st = rrd_stats_create(RRD_TYPE_NET_SNMP, "fragsout", NULL, RRD_TYPE_NET_SNMP, "IPv4 Fragments Sent", "packets/s", 3010, update_every, CHART_TYPE_LINE);
4533 rrd_stats_dimension_add(st, "ok", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4534 rrd_stats_dimension_add(st, "failed", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4535 rrd_stats_dimension_add(st, "all", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4537 else rrd_stats_next(st);
4539 rrd_stats_dimension_set(st, "ok", FragOKs);
4540 rrd_stats_dimension_set(st, "failed", FragFails);
4541 rrd_stats_dimension_set(st, "all", FragCreates);
4545 // --------------------------------------------------------------------
4548 st = rrd_stats_find(RRD_TYPE_NET_SNMP ".fragsin");
4550 st = rrd_stats_create(RRD_TYPE_NET_SNMP, "fragsin", NULL, RRD_TYPE_NET_SNMP, "IPv4 Fragments Reassembly", "packets/s", 3011, update_every, CHART_TYPE_LINE);
4553 rrd_stats_dimension_add(st, "ok", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4554 rrd_stats_dimension_add(st, "failed", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4555 rrd_stats_dimension_add(st, "all", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4557 else rrd_stats_next(st);
4559 rrd_stats_dimension_set(st, "ok", ReasmOKs);
4560 rrd_stats_dimension_set(st, "failed", ReasmFails);
4561 rrd_stats_dimension_set(st, "all", ReasmReqds);
4565 // --------------------------------------------------------------------
4568 st = rrd_stats_find(RRD_TYPE_NET_SNMP ".errors");
4570 st = rrd_stats_create(RRD_TYPE_NET_SNMP, "errors", NULL, RRD_TYPE_NET_SNMP, "IPv4 Errors", "packets/s", 3002, update_every, CHART_TYPE_LINE);
4573 rrd_stats_dimension_add(st, "InDiscards", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4574 rrd_stats_dimension_add(st, "OutDiscards", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4576 rrd_stats_dimension_add(st, "InHdrErrors", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4577 rrd_stats_dimension_add(st, "InAddrErrors", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4578 rrd_stats_dimension_add(st, "InUnknownProtos", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4580 rrd_stats_dimension_add(st, "OutNoRoutes", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4582 else rrd_stats_next(st);
4584 rrd_stats_dimension_set(st, "InDiscards", InDiscards);
4585 rrd_stats_dimension_set(st, "OutDiscards", OutDiscards);
4586 rrd_stats_dimension_set(st, "InHdrErrors", InHdrErrors);
4587 rrd_stats_dimension_set(st, "InAddrErrors", InAddrErrors);
4588 rrd_stats_dimension_set(st, "InUnknownProtos", InUnknownProtos);
4589 rrd_stats_dimension_set(st, "OutNoRoutes", OutNoRoutes);
4593 else if(strncmp(p, "Tcp: ", 5) == 0) {
4594 // skip the header line, read the data
4595 p = fgets(buffer, MAX_PROC_NET_SNMP_LINE, fp);
4598 if(strncmp(p, "Tcp: ", 5) != 0) {
4599 error("Cannot read TCP line from /proc/net/snmp.");
4603 unsigned long long RtoAlgorithm, RtoMin, RtoMax, MaxConn, ActiveOpens, PassiveOpens, AttemptFails, EstabResets,
4604 CurrEstab, InSegs, OutSegs, RetransSegs, InErrs, OutRsts;
4606 int r = sscanf(&buffer[5], "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
4607 &RtoAlgorithm, &RtoMin, &RtoMax, &MaxConn, &ActiveOpens, &PassiveOpens, &AttemptFails, &EstabResets, &CurrEstab, &InSegs, &OutSegs, &RetransSegs, &InErrs, &OutRsts);
4610 if(r != 14) error("Cannot read /proc/net/snmp TCP line. Expected 14 params, read %d.", r);
4612 // --------------------------------------------------------------------
4614 // see http://net-snmp.sourceforge.net/docs/mibs/tcp.html
4615 if(do_tcp_sockets) {
4616 st = rrd_stats_find(RRD_TYPE_NET_SNMP ".tcpsock");
4618 st = rrd_stats_create(RRD_TYPE_NET_SNMP, "tcpsock", NULL, "tcp", "IPv4 TCP Connections", "active connections", 2500, update_every, CHART_TYPE_LINE);
4620 rrd_stats_dimension_add(st, "connections", NULL, 1, 1, RRD_DIMENSION_ABSOLUTE);
4622 else rrd_stats_next(st);
4624 rrd_stats_dimension_set(st, "connections", CurrEstab);
4628 // --------------------------------------------------------------------
4630 if(do_tcp_packets) {
4631 st = rrd_stats_find(RRD_TYPE_NET_SNMP ".tcppackets");
4633 st = rrd_stats_create(RRD_TYPE_NET_SNMP, "tcppackets", NULL, "tcp", "IPv4 TCP Packets", "packets/s", 2600, update_every, CHART_TYPE_LINE);
4635 rrd_stats_dimension_add(st, "received", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4636 rrd_stats_dimension_add(st, "sent", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4638 else rrd_stats_next(st);
4640 rrd_stats_dimension_set(st, "received", InSegs);
4641 rrd_stats_dimension_set(st, "sent", OutSegs);
4645 // --------------------------------------------------------------------
4648 st = rrd_stats_find(RRD_TYPE_NET_SNMP ".tcperrors");
4650 st = rrd_stats_create(RRD_TYPE_NET_SNMP, "tcperrors", NULL, "tcp", "IPv4 TCP Errors", "packets/s", 2700, update_every, CHART_TYPE_LINE);
4653 rrd_stats_dimension_add(st, "InErrs", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4654 rrd_stats_dimension_add(st, "RetransSegs", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4656 else rrd_stats_next(st);
4658 rrd_stats_dimension_set(st, "InErrs", InErrs);
4659 rrd_stats_dimension_set(st, "RetransSegs", RetransSegs);
4663 // --------------------------------------------------------------------
4665 if(do_tcp_handshake) {
4666 st = rrd_stats_find(RRD_TYPE_NET_SNMP ".tcphandshake");
4668 st = rrd_stats_create(RRD_TYPE_NET_SNMP, "tcphandshake", NULL, "tcp", "IPv4 TCP Handshake Issues", "events/s", 2900, update_every, CHART_TYPE_LINE);
4671 rrd_stats_dimension_add(st, "EstabResets", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4672 rrd_stats_dimension_add(st, "OutRsts", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4673 rrd_stats_dimension_add(st, "ActiveOpens", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4674 rrd_stats_dimension_add(st, "PassiveOpens", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4675 rrd_stats_dimension_add(st, "AttemptFails", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4677 else rrd_stats_next(st);
4679 rrd_stats_dimension_set(st, "EstabResets", EstabResets);
4680 rrd_stats_dimension_set(st, "OutRsts", OutRsts);
4681 rrd_stats_dimension_set(st, "ActiveOpens", ActiveOpens);
4682 rrd_stats_dimension_set(st, "PassiveOpens", PassiveOpens);
4683 rrd_stats_dimension_set(st, "AttemptFails", AttemptFails);
4687 else if(strncmp(p, "Udp: ", 5) == 0) {
4688 // skip the header line, read the data
4689 p = fgets(buffer, MAX_PROC_NET_SNMP_LINE, fp);
4692 if(strncmp(p, "Udp: ", 5) != 0) {
4693 error("Cannot read UDP line from /proc/net/snmp.");
4697 unsigned long long InDatagrams, NoPorts, InErrors, OutDatagrams, RcvbufErrors, SndbufErrors;
4699 int r = sscanf(&buffer[5], "%llu %llu %llu %llu %llu %llu\n",
4700 &InDatagrams, &NoPorts, &InErrors, &OutDatagrams, &RcvbufErrors, &SndbufErrors);
4703 if(r != 6) error("Cannot read /proc/net/snmp UDP line. Expected 6 params, read %d.", r);
4705 // --------------------------------------------------------------------
4707 // see http://net-snmp.sourceforge.net/docs/mibs/udp.html
4708 if(do_udp_packets) {
4709 st = rrd_stats_find(RRD_TYPE_NET_SNMP ".udppackets");
4711 st = rrd_stats_create(RRD_TYPE_NET_SNMP, "udppackets", NULL, "udp", "IPv4 UDP Packets", "packets/s", 2601, update_every, CHART_TYPE_LINE);
4713 rrd_stats_dimension_add(st, "received", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4714 rrd_stats_dimension_add(st, "sent", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4716 else rrd_stats_next(st);
4718 rrd_stats_dimension_set(st, "received", InDatagrams);
4719 rrd_stats_dimension_set(st, "sent", OutDatagrams);
4723 // --------------------------------------------------------------------
4726 st = rrd_stats_find(RRD_TYPE_NET_SNMP ".udperrors");
4728 st = rrd_stats_create(RRD_TYPE_NET_SNMP, "udperrors", NULL, "udp", "IPv4 UDP Errors", "events/s", 2701, update_every, CHART_TYPE_LINE);
4731 rrd_stats_dimension_add(st, "RcvbufErrors", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4732 rrd_stats_dimension_add(st, "SndbufErrors", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4733 rrd_stats_dimension_add(st, "InErrors", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4734 rrd_stats_dimension_add(st, "NoPorts", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4736 else rrd_stats_next(st);
4738 rrd_stats_dimension_set(st, "InErrors", InErrors);
4739 rrd_stats_dimension_set(st, "NoPorts", NoPorts);
4740 rrd_stats_dimension_set(st, "RcvbufErrors", RcvbufErrors);
4741 rrd_stats_dimension_set(st, "SndbufErrors", SndbufErrors);
4751 // ----------------------------------------------------------------------------
4752 // /proc/net/netstat processor
4754 int do_proc_net_netstat() {
4755 static int do_bandwidth = -1, do_inerrors = -1, do_mcast = -1, do_bcast = -1, do_mcast_p = -1, do_bcast_p = -1;
4757 if(do_bandwidth == -1) do_bandwidth = config_get_boolean("plugin:proc:/proc/net/netstat", "bandwidth", 1);
4758 if(do_inerrors == -1) do_inerrors = config_get_boolean("plugin:proc:/proc/net/netstat", "input errors", 1);
4759 if(do_mcast == -1) do_mcast = config_get_boolean("plugin:proc:/proc/net/netstat", "multicast bandwidth", 1);
4760 if(do_bcast == -1) do_bcast = config_get_boolean("plugin:proc:/proc/net/netstat", "broadcast bandwidth", 1);
4761 if(do_mcast_p == -1) do_mcast_p = config_get_boolean("plugin:proc:/proc/net/netstat", "multicast packets", 1);
4762 if(do_bcast_p == -1) do_bcast_p = config_get_boolean("plugin:proc:/proc/net/netstat", "broadcast packets", 1);
4764 char buffer[MAX_PROC_NET_SNMP_LINE+1] = "";
4766 FILE *fp = fopen("/proc/net/netstat", "r");
4768 error("Cannot read /proc/net/netstat.");
4773 char *p = fgets(buffer, MAX_PROC_NET_SNMP_LINE, fp);
4776 if(strncmp(p, "IpExt: ", 7) == 0) {
4777 // skip the header line, read the data
4778 p = fgets(buffer, MAX_PROC_NET_SNMP_LINE, fp);
4781 if(strncmp(p, "IpExt: ", 7) != 0) {
4782 error("Cannot read IpExt line from /proc/net/netstat.");
4787 InNoRoutes = 0, InTruncatedPkts = 0,
4788 InOctets = 0, InMcastPkts = 0, InBcastPkts = 0, InMcastOctets = 0, InBcastOctets = 0,
4789 OutOctets = 0, OutMcastPkts = 0, OutBcastPkts = 0, OutMcastOctets = 0, OutBcastOctets = 0;
4791 int r = sscanf(&buffer[7], "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
4792 &InNoRoutes, &InTruncatedPkts, &InMcastPkts, &OutMcastPkts, &InBcastPkts, &OutBcastPkts,
4793 &InOctets, &OutOctets, &InMcastOctets, &OutMcastOctets, &InBcastOctets, &OutBcastOctets);
4797 error("Cannot read /proc/net/netstat IpExt line. Expected 12 params, read %d.", r);
4803 // --------------------------------------------------------------------
4806 st = rrd_stats_find("system.ipv4");
4808 st = rrd_stats_create("system", "ipv4", NULL, "ipv4", "IPv4 Bandwidth", "kilobits/s", 2000, update_every, CHART_TYPE_AREA);
4810 rrd_stats_dimension_add(st, "received", NULL, 8, 1024, RRD_DIMENSION_INCREMENTAL);
4811 rrd_stats_dimension_add(st, "sent", NULL, -8, 1024, RRD_DIMENSION_INCREMENTAL);
4813 else rrd_stats_next(st);
4815 rrd_stats_dimension_set(st, "sent", OutOctets);
4816 rrd_stats_dimension_set(st, "received", InOctets);
4820 // --------------------------------------------------------------------
4823 st = rrd_stats_find("ipv4.inerrors");
4825 st = rrd_stats_create("ipv4", "inerrors", NULL, "ipv4", "IPv4 Input Errors", "packets/s", 4000, update_every, CHART_TYPE_LINE);
4828 rrd_stats_dimension_add(st, "noroutes", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4829 rrd_stats_dimension_add(st, "trunkated", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4831 else rrd_stats_next(st);
4833 rrd_stats_dimension_set(st, "noroutes", InNoRoutes);
4834 rrd_stats_dimension_set(st, "trunkated", InTruncatedPkts);
4838 // --------------------------------------------------------------------
4841 st = rrd_stats_find("ipv4.mcast");
4843 st = rrd_stats_create("ipv4", "mcast", NULL, "ipv4", "IPv4 Multicast Bandwidth", "kilobits/s", 9000, update_every, CHART_TYPE_AREA);
4846 rrd_stats_dimension_add(st, "received", NULL, 8, 1024, RRD_DIMENSION_INCREMENTAL);
4847 rrd_stats_dimension_add(st, "sent", NULL, -8, 1024, RRD_DIMENSION_INCREMENTAL);
4849 else rrd_stats_next(st);
4851 rrd_stats_dimension_set(st, "sent", OutMcastOctets);
4852 rrd_stats_dimension_set(st, "received", InMcastOctets);
4856 // --------------------------------------------------------------------
4859 st = rrd_stats_find("ipv4.bcast");
4861 st = rrd_stats_create("ipv4", "bcast", NULL, "ipv4", "IPv4 Broadcast Bandwidth", "kilobits/s", 8000, update_every, CHART_TYPE_AREA);
4864 rrd_stats_dimension_add(st, "received", NULL, 8, 1024, RRD_DIMENSION_INCREMENTAL);
4865 rrd_stats_dimension_add(st, "sent", NULL, -8, 1024, RRD_DIMENSION_INCREMENTAL);
4867 else rrd_stats_next(st);
4869 rrd_stats_dimension_set(st, "sent", OutBcastOctets);
4870 rrd_stats_dimension_set(st, "received", InBcastOctets);
4874 // --------------------------------------------------------------------
4877 st = rrd_stats_find("ipv4.mcastpkts");
4879 st = rrd_stats_create("ipv4", "mcastpkts", NULL, "ipv4", "IPv4 Multicast Packets", "packets/s", 9500, update_every, CHART_TYPE_LINE);
4882 rrd_stats_dimension_add(st, "received", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4883 rrd_stats_dimension_add(st, "sent", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4885 else rrd_stats_next(st);
4887 rrd_stats_dimension_set(st, "sent", OutMcastPkts);
4888 rrd_stats_dimension_set(st, "received", InMcastPkts);
4892 // --------------------------------------------------------------------
4895 st = rrd_stats_find("ipv4.bcastpkts");
4897 st = rrd_stats_create("ipv4", "bcastpkts", NULL, "ipv4", "IPv4 Broadcast Packets", "packets/s", 8500, update_every, CHART_TYPE_LINE);
4900 rrd_stats_dimension_add(st, "received", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
4901 rrd_stats_dimension_add(st, "sent", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
4903 else rrd_stats_next(st);
4905 rrd_stats_dimension_set(st, "sent", OutBcastPkts);
4906 rrd_stats_dimension_set(st, "received", InBcastPkts);
4916 // ----------------------------------------------------------------------------
4917 // /proc/net/stat/nf_conntrack processor
4919 int do_proc_net_stat_conntrack() {
4920 static int do_sockets = -1, do_new = -1, do_changes = -1, do_expect = -1, do_search = -1, do_errors = -1;
4922 if(do_sockets == -1) do_sockets = config_get_boolean("plugin:proc:/proc/net/stat/nf_conntrack", "netfilter connections", 1);
4923 if(do_new == -1) do_new = config_get_boolean("plugin:proc:/proc/net/stat/nf_conntrack", "netfilter new connections", 1);
4924 if(do_changes == -1) do_changes = config_get_boolean("plugin:proc:/proc/net/stat/nf_conntrack", "netfilter connection changes", 1);
4925 if(do_expect == -1) do_expect = config_get_boolean("plugin:proc:/proc/net/stat/nf_conntrack", "netfilter connection expectations", 1);
4926 if(do_search == -1) do_search = config_get_boolean("plugin:proc:/proc/net/stat/nf_conntrack", "netfilter connection searches", 1);
4927 if(do_errors == -1) do_errors = config_get_boolean("plugin:proc:/proc/net/stat/nf_conntrack", "netfilter errors", 1);
4930 char buffer[MAX_PROC_NET_STAT_CONNTRACK_LINE+1] = "";
4932 FILE *fp = fopen("/proc/net/stat/nf_conntrack", "r");
4934 error("Cannot read /proc/net/stat/nf_conntrack.");
4938 // read and discard the header
4939 char *p = fgets(buffer, MAX_PROC_NET_STAT_CONNTRACK_LINE, fp);
4941 unsigned long long aentries = 0, asearched = 0, afound = 0, anew = 0, ainvalid = 0, aignore = 0, adelete = 0, adelete_list = 0,
4942 ainsert = 0, ainsert_failed = 0, adrop = 0, aearly_drop = 0, aicmp_error = 0, aexpect_new = 0, aexpect_create = 0, aexpect_delete = 0, asearch_restart = 0;
4945 p = fgets(buffer, MAX_PROC_NET_STAT_CONNTRACK_LINE, fp);
4948 unsigned long long tentries = 0, tsearched = 0, tfound = 0, tnew = 0, tinvalid = 0, tignore = 0, tdelete = 0, tdelete_list = 0, tinsert = 0, tinsert_failed = 0, tdrop = 0, tearly_drop = 0, ticmp_error = 0, texpect_new = 0, texpect_create = 0, texpect_delete = 0, tsearch_restart = 0;
4950 int r = sscanf(buffer, "%llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx\n",
4951 &tentries, &tsearched, &tfound, &tnew, &tinvalid, &tignore, &tdelete, &tdelete_list, &tinsert, &tinsert_failed, &tdrop, &tearly_drop, &ticmp_error, &texpect_new, &texpect_create, &texpect_delete, &tsearch_restart);
4954 if(r < 16) error("Cannot read /proc/net/stat/nf_conntrack. Expected 17 params, read %d.", r);
4956 if(!aentries) aentries = tentries;
4958 // sum all the cpus together
4959 asearched += tsearched; // conntrack.search
4960 afound += tfound; // conntrack.search
4961 anew += tnew; // conntrack.new
4962 ainvalid += tinvalid; // conntrack.new
4963 aignore += tignore; // conntrack.new
4964 adelete += tdelete; // conntrack.changes
4965 adelete_list += tdelete_list; // conntrack.changes
4966 ainsert += tinsert; // conntrack.changes
4967 ainsert_failed += tinsert_failed; // conntrack.errors
4968 adrop += tdrop; // conntrack.errors
4969 aearly_drop += tearly_drop; // conntrack.errors
4970 aicmp_error += ticmp_error; // conntrack.errors
4971 aexpect_new += texpect_new; // conntrack.expect
4972 aexpect_create += texpect_create; // conntrack.expect
4973 aexpect_delete += texpect_delete; // conntrack.expect
4974 asearch_restart += tsearch_restart; // conntrack.search
4980 // --------------------------------------------------------------------
4983 st = rrd_stats_find(RRD_TYPE_NET_STAT_CONNTRACK ".sockets");
4985 st = rrd_stats_create(RRD_TYPE_NET_STAT_CONNTRACK, "sockets", NULL, RRD_TYPE_NET_STAT_CONNTRACK, "Netfilter Connections", "active connections", 1000, update_every, CHART_TYPE_LINE);
4987 rrd_stats_dimension_add(st, "connections", NULL, 1, 1, RRD_DIMENSION_ABSOLUTE_NO_INTERPOLATION);
4989 else rrd_stats_next(st);
4991 rrd_stats_dimension_set(st, "connections", aentries);
4995 // --------------------------------------------------------------------
4998 st = rrd_stats_find(RRD_TYPE_NET_STAT_CONNTRACK ".new");
5000 st = rrd_stats_create(RRD_TYPE_NET_STAT_CONNTRACK, "new", NULL, RRD_TYPE_NET_STAT_CONNTRACK, "Netfilter New Connections", "connections/s", 1001, update_every, CHART_TYPE_LINE);
5002 rrd_stats_dimension_add(st, "new", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5003 rrd_stats_dimension_add(st, "ignore", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5004 rrd_stats_dimension_add(st, "invalid", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5006 else rrd_stats_next(st);
5008 rrd_stats_dimension_set(st, "new", anew);
5009 rrd_stats_dimension_set(st, "ignore", aignore);
5010 rrd_stats_dimension_set(st, "invalid", ainvalid);
5014 // --------------------------------------------------------------------
5017 st = rrd_stats_find(RRD_TYPE_NET_STAT_CONNTRACK ".changes");
5019 st = rrd_stats_create(RRD_TYPE_NET_STAT_CONNTRACK, "changes", NULL, RRD_TYPE_NET_STAT_CONNTRACK, "Netfilter Connection Changes", "changes/s", 1002, update_every, CHART_TYPE_LINE);
5022 rrd_stats_dimension_add(st, "inserted", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5023 rrd_stats_dimension_add(st, "deleted", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5024 rrd_stats_dimension_add(st, "delete_list", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5026 else rrd_stats_next(st);
5028 rrd_stats_dimension_set(st, "inserted", ainsert);
5029 rrd_stats_dimension_set(st, "deleted", adelete);
5030 rrd_stats_dimension_set(st, "delete_list", adelete_list);
5034 // --------------------------------------------------------------------
5037 st = rrd_stats_find(RRD_TYPE_NET_STAT_CONNTRACK ".expect");
5039 st = rrd_stats_create(RRD_TYPE_NET_STAT_CONNTRACK, "expect", NULL, RRD_TYPE_NET_STAT_CONNTRACK, "Netfilter Connection Expectations", "expectations/s", 1003, update_every, CHART_TYPE_LINE);
5042 rrd_stats_dimension_add(st, "created", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5043 rrd_stats_dimension_add(st, "deleted", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5044 rrd_stats_dimension_add(st, "new", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5046 else rrd_stats_next(st);
5048 rrd_stats_dimension_set(st, "created", aexpect_create);
5049 rrd_stats_dimension_set(st, "deleted", aexpect_delete);
5050 rrd_stats_dimension_set(st, "new", aexpect_new);
5054 // --------------------------------------------------------------------
5057 st = rrd_stats_find(RRD_TYPE_NET_STAT_CONNTRACK ".search");
5059 st = rrd_stats_create(RRD_TYPE_NET_STAT_CONNTRACK, "search", NULL, RRD_TYPE_NET_STAT_CONNTRACK, "Netfilter Connection Searches", "searches/s", 1010, update_every, CHART_TYPE_LINE);
5062 rrd_stats_dimension_add(st, "searched", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5063 rrd_stats_dimension_add(st, "restarted", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5064 rrd_stats_dimension_add(st, "found", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5066 else rrd_stats_next(st);
5068 rrd_stats_dimension_set(st, "searched", asearched);
5069 rrd_stats_dimension_set(st, "restarted", asearch_restart);
5070 rrd_stats_dimension_set(st, "found", afound);
5074 // --------------------------------------------------------------------
5077 st = rrd_stats_find(RRD_TYPE_NET_STAT_CONNTRACK ".errors");
5079 st = rrd_stats_create(RRD_TYPE_NET_STAT_CONNTRACK, "errors", NULL, RRD_TYPE_NET_STAT_CONNTRACK, "Netfilter Errors", "events/s", 1005, update_every, CHART_TYPE_LINE);
5082 rrd_stats_dimension_add(st, "icmp_error", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5083 rrd_stats_dimension_add(st, "insert_failed", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5084 rrd_stats_dimension_add(st, "drop", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5085 rrd_stats_dimension_add(st, "early_drop", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5087 else rrd_stats_next(st);
5089 rrd_stats_dimension_set(st, "icmp_error", aicmp_error);
5090 rrd_stats_dimension_set(st, "insert_failed", ainsert_failed);
5091 rrd_stats_dimension_set(st, "drop", adrop);
5092 rrd_stats_dimension_set(st, "early_drop", aearly_drop);
5099 // ----------------------------------------------------------------------------
5100 // /proc/net/ip_vs_stats processor
5102 int do_proc_net_ip_vs_stats() {
5103 static int do_bandwidth = -1, do_sockets = -1, do_packets = -1;
5105 if(do_bandwidth == -1) do_bandwidth = config_get_boolean("plugin:proc:/proc/net/ip_vs_stats", "IPVS bandwidth", 1);
5106 if(do_sockets == -1) do_sockets = config_get_boolean("plugin:proc:/proc/net/ip_vs_stats", "IPVS connections", 1);
5107 if(do_packets == -1) do_packets = config_get_boolean("plugin:proc:/proc/net/ip_vs_stats", "IPVS packets", 1);
5109 char buffer[MAX_PROC_NET_IPVS_LINE+1] = "";
5111 FILE *fp = fopen("/proc/net/ip_vs_stats", "r");
5113 error("Cannot read /proc/net/ip_vs_stats.");
5117 // read the discard the 2 header lines
5118 char *p = fgets(buffer, MAX_PROC_NET_IPVS_LINE, fp);
5120 error("Cannot read /proc/net/ip_vs_stats.");
5124 p = fgets(buffer, MAX_PROC_NET_IPVS_LINE, fp);
5126 error("Cannot read /proc/net/ip_vs_stats.");
5130 p = fgets(buffer, MAX_PROC_NET_IPVS_LINE, fp);
5132 error("Cannot read /proc/net/ip_vs_stats.");
5136 unsigned long long entries, InPackets, OutPackets, InBytes, OutBytes;
5138 int r = sscanf(buffer, "%llx %llx %llx %llx %llx\n", &entries, &InPackets, &OutPackets, &InBytes, &OutBytes);
5141 error("Cannot read /proc/net/ip_vs_stats.");
5144 if(r != 5) error("Cannot read /proc/net/ip_vs_stats. Expected 5 params, read %d.", r);
5150 // --------------------------------------------------------------------
5153 st = rrd_stats_find(RRD_TYPE_NET_IPVS ".sockets");
5155 st = rrd_stats_create(RRD_TYPE_NET_IPVS, "sockets", NULL, RRD_TYPE_NET_IPVS, "IPVS New Connections", "connections/s", 1001, update_every, CHART_TYPE_LINE);
5157 rrd_stats_dimension_add(st, "connections", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5159 else rrd_stats_next(st);
5161 rrd_stats_dimension_set(st, "connections", entries);
5165 // --------------------------------------------------------------------
5168 st = rrd_stats_find(RRD_TYPE_NET_IPVS ".packets");
5170 st = rrd_stats_create(RRD_TYPE_NET_IPVS, "packets", NULL, RRD_TYPE_NET_IPVS, "IPVS Packets", "packets/s", 1002, update_every, CHART_TYPE_LINE);
5172 rrd_stats_dimension_add(st, "received", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5173 rrd_stats_dimension_add(st, "sent", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5175 else rrd_stats_next(st);
5177 rrd_stats_dimension_set(st, "received", InPackets);
5178 rrd_stats_dimension_set(st, "sent", OutPackets);
5182 // --------------------------------------------------------------------
5185 st = rrd_stats_find(RRD_TYPE_NET_IPVS ".net");
5187 st = rrd_stats_create(RRD_TYPE_NET_IPVS, "net", NULL, RRD_TYPE_NET_IPVS, "IPVS Bandwidth", "kilobits/s", 1000, update_every, CHART_TYPE_AREA);
5189 rrd_stats_dimension_add(st, "received", NULL, 8, 1024, RRD_DIMENSION_INCREMENTAL);
5190 rrd_stats_dimension_add(st, "sent", NULL, -8, 1024, RRD_DIMENSION_INCREMENTAL);
5192 else rrd_stats_next(st);
5194 rrd_stats_dimension_set(st, "received", InBytes);
5195 rrd_stats_dimension_set(st, "sent", OutBytes);
5202 int do_proc_stat() {
5203 static int do_cpu = -1, do_cpu_cores = -1, do_interrupts = -1, do_context = -1, do_forks = -1, do_processes = -1;
5205 if(do_cpu == -1) do_cpu = config_get_boolean("plugin:proc:/proc/stat", "cpu utilization", 1);
5206 if(do_cpu_cores == -1) do_cpu_cores = config_get_boolean("plugin:proc:/proc/stat", "per cpu core utilization", 1);
5207 if(do_interrupts == -1) do_interrupts = config_get_boolean("plugin:proc:/proc/stat", "cpu interrupts", 1);
5208 if(do_context == -1) do_context = config_get_boolean("plugin:proc:/proc/stat", "context switches", 1);
5209 if(do_forks == -1) do_forks = config_get_boolean("plugin:proc:/proc/stat", "processes started", 1);
5210 if(do_processes == -1) do_processes = config_get_boolean("plugin:proc:/proc/stat", "processes running", 1);
5212 char buffer[MAX_PROC_STAT_LINE+1] = "";
5214 FILE *fp = fopen("/proc/stat", "r");
5216 error("Cannot read /proc/stat.");
5220 unsigned long long processes = 0, running = 0 , blocked = 0;
5224 char *p = fgets(buffer, MAX_PROC_STAT_LINE, fp);
5227 if(strncmp(p, "cpu", 3) == 0) {
5228 char id[MAX_PROC_STAT_NAME + 1] = "";
5229 unsigned long long user = 0, nice = 0, system = 0, idle = 0, iowait = 0, irq = 0, softirq = 0, steal = 0, guest = 0, guest_nice = 0;
5231 int r = sscanf(buffer, "%s %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
5232 id, &user, &nice, &system, &idle, &iowait, &irq, &softirq, &steal, &guest, &guest_nice);
5235 if(r < 10) error("Cannot read /proc/stat cpu line. Expected 11 params, read %d.", r);
5237 char *title = "Core utilization";
5238 char *type = RRD_TYPE_STAT;
5239 long priority = 1000;
5240 int isthistotal = 0;
5241 if(strcmp(id, "cpu") == 0) {
5243 title = "Total CPU utilization";
5248 if((isthistotal && do_cpu) || (!isthistotal && do_cpu_cores)) {
5249 st = rrd_stats_find_bytype(type, id);
5251 st = rrd_stats_create(type, id, NULL, "cpu", title, "percentage", priority, update_every, CHART_TYPE_STACKED);
5253 long multiplier = 1;
5254 long divisor = 1; // sysconf(_SC_CLK_TCK);
5256 rrd_stats_dimension_add(st, "guest_nice", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
5257 rrd_stats_dimension_add(st, "guest", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
5258 rrd_stats_dimension_add(st, "steal", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
5259 rrd_stats_dimension_add(st, "softirq", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
5260 rrd_stats_dimension_add(st, "irq", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
5261 rrd_stats_dimension_add(st, "user", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
5262 rrd_stats_dimension_add(st, "system", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
5263 rrd_stats_dimension_add(st, "nice", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
5264 rrd_stats_dimension_add(st, "iowait", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
5266 rrd_stats_dimension_add(st, "idle", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
5267 rrd_stats_dimension_hide(st, "idle");
5269 else rrd_stats_next(st);
5271 rrd_stats_dimension_set(st, "user", user);
5272 rrd_stats_dimension_set(st, "nice", nice);
5273 rrd_stats_dimension_set(st, "system", system);
5274 rrd_stats_dimension_set(st, "idle", idle);
5275 rrd_stats_dimension_set(st, "iowait", iowait);
5276 rrd_stats_dimension_set(st, "irq", irq);
5277 rrd_stats_dimension_set(st, "softirq", softirq);
5278 rrd_stats_dimension_set(st, "steal", steal);
5279 rrd_stats_dimension_set(st, "guest", guest);
5280 rrd_stats_dimension_set(st, "guest_nice", guest_nice);
5284 else if(strncmp(p, "intr ", 5) == 0) {
5285 char id[MAX_PROC_STAT_NAME + 1];
5287 unsigned long long value;
5289 int r = sscanf(buffer, "%s %llu ", id, &value);
5291 if(r != 2) error("Cannot read /proc/stat intr line. Expected 2 params, read %d.", r);
5293 // --------------------------------------------------------------------
5296 st = rrd_stats_find_bytype("system", id);
5298 st = rrd_stats_create("system", id, NULL, "cpu", "CPU Interrupts", "interrupts/s", 900, update_every, CHART_TYPE_LINE);
5301 rrd_stats_dimension_add(st, "interrupts", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5303 else rrd_stats_next(st);
5305 rrd_stats_dimension_set(st, "interrupts", value);
5309 else if(strncmp(p, "ctxt ", 5) == 0) {
5310 char id[MAX_PROC_STAT_NAME + 1] = "";
5312 unsigned long long value;
5314 int r = sscanf(buffer, "%s %llu ", id, &value);
5316 if(r != 2) error("Cannot read /proc/stat ctxt line. Expected 2 params, read %d.", r);
5318 // --------------------------------------------------------------------
5321 st = rrd_stats_find_bytype("system", id);
5323 st = rrd_stats_create("system", id, NULL, "cpu", "CPU Context Switches", "context switches/s", 800, update_every, CHART_TYPE_LINE);
5325 rrd_stats_dimension_add(st, "switches", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5327 else rrd_stats_next(st);
5329 rrd_stats_dimension_set(st, "switches", value);
5333 else if(strncmp(p, "processes ", 10) == 0) {
5334 char id[MAX_PROC_STAT_NAME + 1] = "";
5336 unsigned long long value;
5338 int r = sscanf(buffer, "%s %llu ", id, &value);
5340 if(r != 2) error("Cannot read /proc/stat processes line. Expected 2 params, read %d.", r);
5344 else if(strncmp(p, "procs_running ", 14) == 0) {
5345 char id[MAX_PROC_STAT_NAME + 1] = "";
5347 unsigned long long value;
5349 int r = sscanf(buffer, "%s %llu ", id, &value);
5351 if(r != 2) error("Cannot read /proc/stat procs_running line. Expected 2 params, read %d.", r);
5355 else if(strncmp(p, "procs_blocked ", 14) == 0) {
5356 char id[MAX_PROC_STAT_NAME + 1] = "procs_running";
5358 unsigned long long value;
5360 int r = sscanf(buffer, "%s %llu ", id, &value);
5362 if(r != 2) error("Cannot read /proc/stat procs_blocked line. Expected 2 params, read %d.", r);
5369 // --------------------------------------------------------------------
5372 st = rrd_stats_find_bytype("system", "forks");
5374 st = rrd_stats_create("system", "forks", NULL, "cpu", "New Processes", "processes/s", 700, update_every, CHART_TYPE_LINE);
5377 rrd_stats_dimension_add(st, "started", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5379 else rrd_stats_next(st);
5381 rrd_stats_dimension_set(st, "started", processes);
5385 // --------------------------------------------------------------------
5388 st = rrd_stats_find_bytype("system", "processes");
5390 st = rrd_stats_create("system", "processes", NULL, "cpu", "Processes", "processes", 600, update_every, CHART_TYPE_LINE);
5392 rrd_stats_dimension_add(st, "running", NULL, 1, 1, RRD_DIMENSION_ABSOLUTE_NO_INTERPOLATION);
5393 rrd_stats_dimension_add(st, "blocked", NULL, -1, 1, RRD_DIMENSION_ABSOLUTE_NO_INTERPOLATION);
5395 else rrd_stats_next(st);
5397 rrd_stats_dimension_set(st, "running", running);
5398 rrd_stats_dimension_set(st, "blocked", blocked);
5405 // ----------------------------------------------------------------------------
5406 // /proc/meminfo processor
5408 #define MAX_PROC_MEMINFO_LINE 4096
5409 #define MAX_PROC_MEMINFO_NAME 1024
5411 int do_proc_meminfo() {
5412 static int do_ram = -1, do_swap = -1, do_hwcorrupt = -1, do_committed = -1, do_writeback = -1, do_kernel = -1, do_slab = -1;
5414 if(do_ram == -1) do_ram = config_get_boolean("plugin:proc:/proc/meminfo", "system ram", 1);
5415 if(do_swap == -1) do_swap = config_get_boolean("plugin:proc:/proc/meminfo", "system swap", 1);
5416 if(do_hwcorrupt == -1) do_hwcorrupt = config_get_boolean("plugin:proc:/proc/meminfo", "hardware corrupted ECC", 1);
5417 if(do_committed == -1) do_committed = config_get_boolean("plugin:proc:/proc/meminfo", "committed memory", 1);
5418 if(do_writeback == -1) do_writeback = config_get_boolean("plugin:proc:/proc/meminfo", "writeback memory", 1);
5419 if(do_kernel == -1) do_kernel = config_get_boolean("plugin:proc:/proc/meminfo", "kernel memory", 1);
5420 if(do_slab == -1) do_slab = config_get_boolean("plugin:proc:/proc/meminfo", "slab memory", 1);
5422 char buffer[MAX_PROC_MEMINFO_LINE+1] = "";
5424 FILE *fp = fopen("/proc/meminfo", "r");
5426 error("Cannot read /proc/meminfo.");
5430 int hwcorrupted = 0;
5432 unsigned long long MemTotal = 0, MemFree = 0, Buffers = 0, Cached = 0, SwapCached = 0,
5433 Active = 0, Inactive = 0, ActiveAnon = 0, InactiveAnon = 0, ActiveFile = 0, InactiveFile = 0,
5434 Unevictable = 0, Mlocked = 0, SwapTotal = 0, SwapFree = 0, Dirty = 0, Writeback = 0, AnonPages = 0,
5435 Mapped = 0, Shmem = 0, Slab = 0, SReclaimable = 0, SUnreclaim = 0, KernelStack = 0, PageTables = 0,
5436 NFS_Unstable = 0, Bounce = 0, WritebackTmp = 0, CommitLimit = 0, Committed_AS = 0,
5437 VmallocTotal = 0, VmallocUsed = 0, VmallocChunk = 0,
5438 AnonHugePages = 0, HugePages_Total = 0, HugePages_Free = 0, HugePages_Rsvd = 0, HugePages_Surp = 0, Hugepagesize = 0,
5439 DirectMap4k = 0, DirectMap2M = 0, HardwareCorrupted = 0;
5442 char *p = fgets(buffer, MAX_PROC_MEMINFO_LINE, fp);
5446 while((p = strchr(buffer, ':'))) *p = ' ';
5448 char name[MAX_PROC_MEMINFO_NAME + 1] = "";
5449 unsigned long long value = 0;
5451 int r = sscanf(buffer, "%s %llu kB\n", name, &value);
5454 if(r != 2) error("Cannot read /proc/meminfo line. Expected 2 params, read %d.", r);
5456 if(!MemTotal && strcmp(name, "MemTotal") == 0) MemTotal = value;
5457 else if(!MemFree && strcmp(name, "MemFree") == 0) MemFree = value;
5458 else if(!Buffers && strcmp(name, "Buffers") == 0) Buffers = value;
5459 else if(!Cached && strcmp(name, "Cached") == 0) Cached = value;
5460 else if(!SwapCached && strcmp(name, "SwapCached") == 0) SwapCached = value;
5461 else if(!Active && strcmp(name, "Active") == 0) Active = value;
5462 else if(!Inactive && strcmp(name, "Inactive") == 0) Inactive = value;
5463 else if(!ActiveAnon && strcmp(name, "ActiveAnon") == 0) ActiveAnon = value;
5464 else if(!InactiveAnon && strcmp(name, "InactiveAnon") == 0) InactiveAnon = value;
5465 else if(!ActiveFile && strcmp(name, "ActiveFile") == 0) ActiveFile = value;
5466 else if(!InactiveFile && strcmp(name, "InactiveFile") == 0) InactiveFile = value;
5467 else if(!Unevictable && strcmp(name, "Unevictable") == 0) Unevictable = value;
5468 else if(!Mlocked && strcmp(name, "Mlocked") == 0) Mlocked = value;
5469 else if(!SwapTotal && strcmp(name, "SwapTotal") == 0) SwapTotal = value;
5470 else if(!SwapFree && strcmp(name, "SwapFree") == 0) SwapFree = value;
5471 else if(!Dirty && strcmp(name, "Dirty") == 0) Dirty = value;
5472 else if(!Writeback && strcmp(name, "Writeback") == 0) Writeback = value;
5473 else if(!AnonPages && strcmp(name, "AnonPages") == 0) AnonPages = value;
5474 else if(!Mapped && strcmp(name, "Mapped") == 0) Mapped = value;
5475 else if(!Shmem && strcmp(name, "Shmem") == 0) Shmem = value;
5476 else if(!Slab && strcmp(name, "Slab") == 0) Slab = value;
5477 else if(!SReclaimable && strcmp(name, "SReclaimable") == 0) SReclaimable = value;
5478 else if(!SUnreclaim && strcmp(name, "SUnreclaim") == 0) SUnreclaim = value;
5479 else if(!KernelStack && strcmp(name, "KernelStack") == 0) KernelStack = value;
5480 else if(!PageTables && strcmp(name, "PageTables") == 0) PageTables = value;
5481 else if(!NFS_Unstable && strcmp(name, "NFS_Unstable") == 0) NFS_Unstable = value;
5482 else if(!Bounce && strcmp(name, "Bounce") == 0) Bounce = value;
5483 else if(!WritebackTmp && strcmp(name, "WritebackTmp") == 0) WritebackTmp = value;
5484 else if(!CommitLimit && strcmp(name, "CommitLimit") == 0) CommitLimit = value;
5485 else if(!Committed_AS && strcmp(name, "Committed_AS") == 0) Committed_AS = value;
5486 else if(!VmallocTotal && strcmp(name, "VmallocTotal") == 0) VmallocTotal = value;
5487 else if(!VmallocUsed && strcmp(name, "VmallocUsed") == 0) VmallocUsed = value;
5488 else if(!VmallocChunk && strcmp(name, "VmallocChunk") == 0) VmallocChunk = value;
5489 else if(!HardwareCorrupted && strcmp(name, "HardwareCorrupted") == 0) { HardwareCorrupted = value; hwcorrupted = 1; }
5490 else if(!AnonHugePages && strcmp(name, "AnonHugePages") == 0) AnonHugePages = value;
5491 else if(!HugePages_Total && strcmp(name, "HugePages_Total") == 0) HugePages_Total = value;
5492 else if(!HugePages_Free && strcmp(name, "HugePages_Free") == 0) HugePages_Free = value;
5493 else if(!HugePages_Rsvd && strcmp(name, "HugePages_Rsvd") == 0) HugePages_Rsvd = value;
5494 else if(!HugePages_Surp && strcmp(name, "HugePages_Surp") == 0) HugePages_Surp = value;
5495 else if(!Hugepagesize && strcmp(name, "Hugepagesize") == 0) Hugepagesize = value;
5496 else if(!DirectMap4k && strcmp(name, "DirectMap4k") == 0) DirectMap4k = value;
5497 else if(!DirectMap2M && strcmp(name, "DirectMap2M") == 0) DirectMap2M = value;
5503 // --------------------------------------------------------------------
5505 // http://stackoverflow.com/questions/3019748/how-to-reliably-measure-available-memory-in-linux
5506 unsigned long long MemUsed = MemTotal - MemFree - Cached - Buffers;
5509 st = rrd_stats_find("system.ram");
5511 st = rrd_stats_create("system", "ram", NULL, "mem", "System RAM", "MB", 200, update_every, CHART_TYPE_STACKED);
5513 rrd_stats_dimension_add(st, "buffers", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5514 rrd_stats_dimension_add(st, "used", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5515 rrd_stats_dimension_add(st, "cached", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5516 rrd_stats_dimension_add(st, "free", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5518 else rrd_stats_next(st);
5520 rrd_stats_dimension_set(st, "used", MemUsed);
5521 rrd_stats_dimension_set(st, "free", MemFree);
5522 rrd_stats_dimension_set(st, "cached", Cached);
5523 rrd_stats_dimension_set(st, "buffers", Buffers);
5527 // --------------------------------------------------------------------
5529 unsigned long long SwapUsed = SwapTotal - SwapFree;
5532 st = rrd_stats_find("system.swap");
5534 st = rrd_stats_create("system", "swap", NULL, "mem", "System Swap", "MB", 201, update_every, CHART_TYPE_STACKED);
5537 rrd_stats_dimension_add(st, "free", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5538 rrd_stats_dimension_add(st, "used", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5540 else rrd_stats_next(st);
5542 rrd_stats_dimension_set(st, "used", SwapUsed);
5543 rrd_stats_dimension_set(st, "free", SwapFree);
5547 // --------------------------------------------------------------------
5549 if(hwcorrupted && do_hwcorrupt) {
5550 st = rrd_stats_find("mem.hwcorrupt");
5552 st = rrd_stats_create("mem", "hwcorrupt", NULL, "mem", "Hardware Corrupted ECC", "MB", 9000, update_every, CHART_TYPE_LINE);
5555 rrd_stats_dimension_add(st, "HardwareCorrupted", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5557 else rrd_stats_next(st);
5559 rrd_stats_dimension_set(st, "HardwareCorrupted", HardwareCorrupted);
5563 // --------------------------------------------------------------------
5566 st = rrd_stats_find("mem.committed");
5568 st = rrd_stats_create("mem", "committed", NULL, "mem", "Committed (Allocated) Memory", "MB", 5000, update_every, CHART_TYPE_AREA);
5571 rrd_stats_dimension_add(st, "Committed_AS", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5573 else rrd_stats_next(st);
5575 rrd_stats_dimension_set(st, "Committed_AS", Committed_AS);
5579 // --------------------------------------------------------------------
5582 st = rrd_stats_find("mem.writeback");
5584 st = rrd_stats_create("mem", "writeback", NULL, "mem", "Writeback Memory", "MB", 4000, update_every, CHART_TYPE_LINE);
5587 rrd_stats_dimension_add(st, "Dirty", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5588 rrd_stats_dimension_add(st, "Writeback", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5589 rrd_stats_dimension_add(st, "FuseWriteback", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5590 rrd_stats_dimension_add(st, "NfsWriteback", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5591 rrd_stats_dimension_add(st, "Bounce", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5593 else rrd_stats_next(st);
5595 rrd_stats_dimension_set(st, "Dirty", Dirty);
5596 rrd_stats_dimension_set(st, "Writeback", Writeback);
5597 rrd_stats_dimension_set(st, "FuseWriteback", WritebackTmp);
5598 rrd_stats_dimension_set(st, "NfsWriteback", NFS_Unstable);
5599 rrd_stats_dimension_set(st, "Bounce", Bounce);
5603 // --------------------------------------------------------------------
5606 st = rrd_stats_find("mem.kernel");
5608 st = rrd_stats_create("mem", "kernel", NULL, "mem", "Memory Used by Kernel", "MB", 6000, update_every, CHART_TYPE_STACKED);
5611 rrd_stats_dimension_add(st, "Slab", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5612 rrd_stats_dimension_add(st, "KernelStack", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5613 rrd_stats_dimension_add(st, "PageTables", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5614 rrd_stats_dimension_add(st, "VmallocUsed", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5616 else rrd_stats_next(st);
5618 rrd_stats_dimension_set(st, "KernelStack", KernelStack);
5619 rrd_stats_dimension_set(st, "Slab", Slab);
5620 rrd_stats_dimension_set(st, "PageTables", PageTables);
5621 rrd_stats_dimension_set(st, "VmallocUsed", VmallocUsed);
5625 // --------------------------------------------------------------------
5628 st = rrd_stats_find("mem.slab");
5630 st = rrd_stats_create("mem", "slab", NULL, "mem", "Reclaimable Kernel Memory", "MB", 6500, update_every, CHART_TYPE_STACKED);
5633 rrd_stats_dimension_add(st, "reclaimable", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5634 rrd_stats_dimension_add(st, "unreclaimable", NULL, 1, 1024, RRD_DIMENSION_ABSOLUTE);
5636 else rrd_stats_next(st);
5638 rrd_stats_dimension_set(st, "reclaimable", SReclaimable);
5639 rrd_stats_dimension_set(st, "unreclaimable", SUnreclaim);
5646 // ----------------------------------------------------------------------------
5647 // /proc/vmstat processor
5649 #define MAX_PROC_VMSTAT_LINE 4096
5650 #define MAX_PROC_VMSTAT_NAME 1024
5652 int do_proc_vmstat() {
5653 static int do_swapio = -1, do_io = -1, do_pgfaults = -1;
5655 if(do_swapio == -1) do_swapio = config_get_boolean("plugin:proc:/proc/vmstat", "swap i/o", 1);
5656 if(do_io == -1) do_io = config_get_boolean("plugin:proc:/proc/vmstat", "disk i/o", 1);
5657 if(do_pgfaults == -1) do_pgfaults = config_get_boolean("plugin:proc:/proc/vmstat", "memory page faults", 1);
5659 char buffer[MAX_PROC_VMSTAT_LINE+1] = "";
5661 FILE *fp = fopen("/proc/vmstat", "r");
5663 error("Cannot read /proc/vmstat.");
5667 unsigned long long nr_free_pages = 0, nr_inactive_anon = 0, nr_active_anon = 0, nr_inactive_file = 0, nr_active_file = 0, nr_unevictable = 0, nr_mlock = 0,
5668 nr_anon_pages = 0, nr_mapped = 0, nr_file_pages = 0, nr_dirty = 0, nr_writeback = 0, nr_slab_reclaimable = 0, nr_slab_unreclaimable = 0, nr_page_table_pages = 0,
5669 nr_kernel_stack = 0, nr_unstable = 0, nr_bounce = 0, nr_vmscan_write = 0, nr_vmscan_immediate_reclaim = 0, nr_writeback_temp = 0, nr_isolated_anon = 0, nr_isolated_file = 0,
5670 nr_shmem = 0, nr_dirtied = 0, nr_written = 0, nr_anon_transparent_hugepages = 0, nr_dirty_threshold = 0, nr_dirty_background_threshold = 0,
5671 pgpgin = 0, pgpgout = 0, pswpin = 0, pswpout = 0, pgalloc_dma = 0, pgalloc_dma32 = 0, pgalloc_normal = 0, pgalloc_movable = 0, pgfree = 0, pgactivate = 0, pgdeactivate = 0,
5672 pgfault = 0, pgmajfault = 0, pgrefill_dma = 0, pgrefill_dma32 = 0, pgrefill_normal = 0, pgrefill_movable = 0, pgsteal_kswapd_dma = 0, pgsteal_kswapd_dma32 = 0,
5673 pgsteal_kswapd_normal = 0, pgsteal_kswapd_movable = 0, pgsteal_direct_dma = 0, pgsteal_direct_dma32 = 0, pgsteal_direct_normal = 0, pgsteal_direct_movable = 0,
5674 pgscan_kswapd_dma = 0, pgscan_kswapd_dma32 = 0, pgscan_kswapd_normal = 0, pgscan_kswapd_movable = 0, pgscan_direct_dma = 0, pgscan_direct_dma32 = 0, pgscan_direct_normal = 0,
5675 pgscan_direct_movable = 0, pginodesteal = 0, slabs_scanned = 0, kswapd_inodesteal = 0, kswapd_low_wmark_hit_quickly = 0, kswapd_high_wmark_hit_quickly = 0,
5676 kswapd_skip_congestion_wait = 0, pageoutrun = 0, allocstall = 0, pgrotated = 0, compact_blocks_moved = 0, compact_pages_moved = 0, compact_pagemigrate_failed = 0,
5677 compact_stall = 0, compact_fail = 0, compact_success = 0, htlb_buddy_alloc_success = 0, htlb_buddy_alloc_fail = 0, unevictable_pgs_culled = 0, unevictable_pgs_scanned = 0,
5678 unevictable_pgs_rescued = 0, unevictable_pgs_mlocked = 0, unevictable_pgs_munlocked = 0, unevictable_pgs_cleared = 0, unevictable_pgs_stranded = 0, unevictable_pgs_mlockfreed = 0,
5679 thp_fault_alloc = 0, thp_fault_fallback = 0, thp_collapse_alloc = 0, thp_collapse_alloc_failed = 0, thp_split = 0;
5682 char *p = fgets(buffer, MAX_PROC_VMSTAT_NAME, fp);
5686 while((p = strchr(buffer, ':'))) *p = ' ';
5688 char name[MAX_PROC_MEMINFO_NAME + 1] = "";
5689 unsigned long long value = 0;
5691 int r = sscanf(buffer, "%s %llu\n", name, &value);
5694 if(r != 2) error("Cannot read /proc/meminfo line. Expected 2 params, read %d.", r);
5696 if(!nr_free_pages && strcmp(name, "nr_free_pages") == 0) nr_free_pages = value;
5697 else if(!nr_inactive_anon && strcmp(name, "nr_inactive_anon") == 0) nr_inactive_anon = value;
5698 else if(!nr_active_anon && strcmp(name, "nr_active_anon") == 0) nr_active_anon = value;
5699 else if(!nr_inactive_file && strcmp(name, "nr_inactive_file") == 0) nr_inactive_file = value;
5700 else if(!nr_active_file && strcmp(name, "nr_active_file") == 0) nr_active_file = value;
5701 else if(!nr_unevictable && strcmp(name, "nr_unevictable") == 0) nr_unevictable = value;
5702 else if(!nr_mlock && strcmp(name, "nr_mlock") == 0) nr_mlock = value;
5703 else if(!nr_anon_pages && strcmp(name, "nr_anon_pages") == 0) nr_anon_pages = value;
5704 else if(!nr_mapped && strcmp(name, "nr_mapped") == 0) nr_mapped = value;
5705 else if(!nr_file_pages && strcmp(name, "nr_file_pages") == 0) nr_file_pages = value;
5706 else if(!nr_dirty && strcmp(name, "nr_dirty") == 0) nr_dirty = value;
5707 else if(!nr_writeback && strcmp(name, "nr_writeback") == 0) nr_writeback = value;
5708 else if(!nr_slab_reclaimable && strcmp(name, "nr_slab_reclaimable") == 0) nr_slab_reclaimable = value;
5709 else if(!nr_slab_unreclaimable && strcmp(name, "nr_slab_unreclaimable") == 0) nr_slab_unreclaimable = value;
5710 else if(!nr_page_table_pages && strcmp(name, "nr_page_table_pages") == 0) nr_page_table_pages = value;
5711 else if(!nr_kernel_stack && strcmp(name, "nr_kernel_stack") == 0) nr_kernel_stack = value;
5712 else if(!nr_unstable && strcmp(name, "nr_unstable") == 0) nr_unstable = value;
5713 else if(!nr_bounce && strcmp(name, "nr_bounce") == 0) nr_bounce = value;
5714 else if(!nr_vmscan_write && strcmp(name, "nr_vmscan_write") == 0) nr_vmscan_write = value;
5715 else if(!nr_vmscan_immediate_reclaim && strcmp(name, "nr_vmscan_immediate_reclaim") == 0) nr_vmscan_immediate_reclaim = value;
5716 else if(!nr_writeback_temp && strcmp(name, "nr_writeback_temp") == 0) nr_writeback_temp = value;
5717 else if(!nr_isolated_anon && strcmp(name, "nr_isolated_anon") == 0) nr_isolated_anon = value;
5718 else if(!nr_isolated_file && strcmp(name, "nr_isolated_file") == 0) nr_isolated_file = value;
5719 else if(!nr_shmem && strcmp(name, "nr_shmem") == 0) nr_shmem = value;
5720 else if(!nr_dirtied && strcmp(name, "nr_dirtied") == 0) nr_dirtied = value;
5721 else if(!nr_written && strcmp(name, "nr_written") == 0) nr_written = value;
5722 else if(!nr_anon_transparent_hugepages && strcmp(name, "nr_anon_transparent_hugepages") == 0) nr_anon_transparent_hugepages = value;
5723 else if(!nr_dirty_threshold && strcmp(name, "nr_dirty_threshold") == 0) nr_dirty_threshold = value;
5724 else if(!nr_dirty_background_threshold && strcmp(name, "nr_dirty_background_threshold") == 0) nr_dirty_background_threshold = value;
5725 else if(!pgpgin && strcmp(name, "pgpgin") == 0) pgpgin = value;
5726 else if(!pgpgout && strcmp(name, "pgpgout") == 0) pgpgout = value;
5727 else if(!pswpin && strcmp(name, "pswpin") == 0) pswpin = value;
5728 else if(!pswpout && strcmp(name, "pswpout") == 0) pswpout = value;
5729 else if(!pgalloc_dma && strcmp(name, "pgalloc_dma") == 0) pgalloc_dma = value;
5730 else if(!pgalloc_dma32 && strcmp(name, "pgalloc_dma32") == 0) pgalloc_dma32 = value;
5731 else if(!pgalloc_normal && strcmp(name, "pgalloc_normal") == 0) pgalloc_normal = value;
5732 else if(!pgalloc_movable && strcmp(name, "pgalloc_movable") == 0) pgalloc_movable = value;
5733 else if(!pgfree && strcmp(name, "pgfree") == 0) pgfree = value;
5734 else if(!pgactivate && strcmp(name, "pgactivate") == 0) pgactivate = value;
5735 else if(!pgdeactivate && strcmp(name, "pgdeactivate") == 0) pgdeactivate = value;
5736 else if(!pgfault && strcmp(name, "pgfault") == 0) pgfault = value;
5737 else if(!pgmajfault && strcmp(name, "pgmajfault") == 0) pgmajfault = value;
5738 else if(!pgrefill_dma && strcmp(name, "pgrefill_dma") == 0) pgrefill_dma = value;
5739 else if(!pgrefill_dma32 && strcmp(name, "pgrefill_dma32") == 0) pgrefill_dma32 = value;
5740 else if(!pgrefill_normal && strcmp(name, "pgrefill_normal") == 0) pgrefill_normal = value;
5741 else if(!pgrefill_movable && strcmp(name, "pgrefill_movable") == 0) pgrefill_movable = value;
5742 else if(!pgsteal_kswapd_dma && strcmp(name, "pgsteal_kswapd_dma") == 0) pgsteal_kswapd_dma = value;
5743 else if(!pgsteal_kswapd_dma32 && strcmp(name, "pgsteal_kswapd_dma32") == 0) pgsteal_kswapd_dma32 = value;
5744 else if(!pgsteal_kswapd_normal && strcmp(name, "pgsteal_kswapd_normal") == 0) pgsteal_kswapd_normal = value;
5745 else if(!pgsteal_kswapd_movable && strcmp(name, "pgsteal_kswapd_movable") == 0) pgsteal_kswapd_movable = value;
5746 else if(!pgsteal_direct_dma && strcmp(name, "pgsteal_direct_dma") == 0) pgsteal_direct_dma = value;
5747 else if(!pgsteal_direct_dma32 && strcmp(name, "pgsteal_direct_dma32") == 0) pgsteal_direct_dma32 = value;
5748 else if(!pgsteal_direct_normal && strcmp(name, "pgsteal_direct_normal") == 0) pgsteal_direct_normal = value;
5749 else if(!pgsteal_direct_movable && strcmp(name, "pgsteal_direct_movable") == 0) pgsteal_direct_movable = value;
5750 else if(!pgscan_kswapd_dma && strcmp(name, "pgscan_kswapd_dma") == 0) pgscan_kswapd_dma = value;
5751 else if(!pgscan_kswapd_dma32 && strcmp(name, "pgscan_kswapd_dma32") == 0) pgscan_kswapd_dma32 = value;
5752 else if(!pgscan_kswapd_normal && strcmp(name, "pgscan_kswapd_normal") == 0) pgscan_kswapd_normal = value;
5753 else if(!pgscan_kswapd_movable && strcmp(name, "pgscan_kswapd_movable") == 0) pgscan_kswapd_movable = value;
5754 else if(!pgscan_direct_dma && strcmp(name, "pgscan_direct_dma") == 0) pgscan_direct_dma = value;
5755 else if(!pgscan_direct_dma32 && strcmp(name, "pgscan_direct_dma32") == 0) pgscan_direct_dma32 = value;
5756 else if(!pgscan_direct_normal && strcmp(name, "pgscan_direct_normal") == 0) pgscan_direct_normal = value;
5757 else if(!pgscan_direct_movable && strcmp(name, "pgscan_direct_movable") == 0) pgscan_direct_movable = value;
5758 else if(!pginodesteal && strcmp(name, "pginodesteal") == 0) pginodesteal = value;
5759 else if(!slabs_scanned && strcmp(name, "slabs_scanned") == 0) slabs_scanned = value;
5760 else if(!kswapd_inodesteal && strcmp(name, "kswapd_inodesteal") == 0) kswapd_inodesteal = value;
5761 else if(!kswapd_low_wmark_hit_quickly && strcmp(name, "kswapd_low_wmark_hit_quickly") == 0) kswapd_low_wmark_hit_quickly = value;
5762 else if(!kswapd_high_wmark_hit_quickly && strcmp(name, "kswapd_high_wmark_hit_quickly") == 0) kswapd_high_wmark_hit_quickly = value;
5763 else if(!kswapd_skip_congestion_wait && strcmp(name, "kswapd_skip_congestion_wait") == 0) kswapd_skip_congestion_wait = value;
5764 else if(!pageoutrun && strcmp(name, "pageoutrun") == 0) pageoutrun = value;
5765 else if(!allocstall && strcmp(name, "allocstall") == 0) allocstall = value;
5766 else if(!pgrotated && strcmp(name, "pgrotated") == 0) pgrotated = value;
5767 else if(!compact_blocks_moved && strcmp(name, "compact_blocks_moved") == 0) compact_blocks_moved = value;
5768 else if(!compact_pages_moved && strcmp(name, "compact_pages_moved") == 0) compact_pages_moved = value;
5769 else if(!compact_pagemigrate_failed && strcmp(name, "compact_pagemigrate_failed") == 0) compact_pagemigrate_failed = value;
5770 else if(!compact_stall && strcmp(name, "compact_stall") == 0) compact_stall = value;
5771 else if(!compact_fail && strcmp(name, "compact_fail") == 0) compact_fail = value;
5772 else if(!compact_success && strcmp(name, "compact_success") == 0) compact_success = value;
5773 else if(!htlb_buddy_alloc_success && strcmp(name, "htlb_buddy_alloc_success") == 0) htlb_buddy_alloc_success = value;
5774 else if(!htlb_buddy_alloc_fail && strcmp(name, "htlb_buddy_alloc_fail") == 0) htlb_buddy_alloc_fail = value;
5775 else if(!unevictable_pgs_culled && strcmp(name, "unevictable_pgs_culled") == 0) unevictable_pgs_culled = value;
5776 else if(!unevictable_pgs_scanned && strcmp(name, "unevictable_pgs_scanned") == 0) unevictable_pgs_scanned = value;
5777 else if(!unevictable_pgs_rescued && strcmp(name, "unevictable_pgs_rescued") == 0) unevictable_pgs_rescued = value;
5778 else if(!unevictable_pgs_mlocked && strcmp(name, "unevictable_pgs_mlocked") == 0) unevictable_pgs_mlocked = value;
5779 else if(!unevictable_pgs_munlocked && strcmp(name, "unevictable_pgs_munlocked") == 0) unevictable_pgs_munlocked = value;
5780 else if(!unevictable_pgs_cleared && strcmp(name, "unevictable_pgs_cleared") == 0) unevictable_pgs_cleared = value;
5781 else if(!unevictable_pgs_stranded && strcmp(name, "unevictable_pgs_stranded") == 0) unevictable_pgs_stranded = value;
5782 else if(!unevictable_pgs_mlockfreed && strcmp(name, "unevictable_pgs_mlockfreed") == 0) unevictable_pgs_mlockfreed = value;
5783 else if(!thp_fault_alloc && strcmp(name, "thp_fault_alloc") == 0) thp_fault_alloc = value;
5784 else if(!thp_fault_fallback && strcmp(name, "thp_fault_fallback") == 0) thp_fault_fallback = value;
5785 else if(!thp_collapse_alloc && strcmp(name, "thp_collapse_alloc") == 0) thp_collapse_alloc = value;
5786 else if(!thp_collapse_alloc_failed && strcmp(name, "thp_collapse_alloc_failed") == 0) thp_collapse_alloc_failed = value;
5787 else if(!thp_split && strcmp(name, "thp_split") == 0) thp_split = value;
5793 // --------------------------------------------------------------------
5796 st = rrd_stats_find("system.swapio");
5798 st = rrd_stats_create("system", "swapio", NULL, "mem", "Swap I/O", "kilobytes/s", 250, update_every, CHART_TYPE_AREA);
5800 rrd_stats_dimension_add(st, "in", NULL, sysconf(_SC_PAGESIZE), 1024, RRD_DIMENSION_INCREMENTAL);
5801 rrd_stats_dimension_add(st, "out", NULL, -sysconf(_SC_PAGESIZE), 1024, RRD_DIMENSION_INCREMENTAL);
5803 else rrd_stats_next(st);
5805 rrd_stats_dimension_set(st, "in", pswpin);
5806 rrd_stats_dimension_set(st, "out", pswpout);
5810 // --------------------------------------------------------------------
5813 st = rrd_stats_find("system.io");
5815 st = rrd_stats_create("system", "io", NULL, "disk", "Disk I/O", "kilobytes/s", 150, update_every, CHART_TYPE_AREA);
5817 rrd_stats_dimension_add(st, "in", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
5818 rrd_stats_dimension_add(st, "out", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
5820 else rrd_stats_next(st);
5822 rrd_stats_dimension_set(st, "in", pgpgin);
5823 rrd_stats_dimension_set(st, "out", pgpgout);
5827 // --------------------------------------------------------------------
5830 st = rrd_stats_find("system.pgfaults");
5832 st = rrd_stats_create("system", "pgfaults", NULL, "mem", "Memory Page Faults", "page faults/s", 500, update_every, CHART_TYPE_LINE);
5835 rrd_stats_dimension_add(st, "minor", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
5836 rrd_stats_dimension_add(st, "major", NULL, -1, 1, RRD_DIMENSION_INCREMENTAL);
5838 else rrd_stats_next(st);
5840 rrd_stats_dimension_set(st, "minor", pgfault);
5841 rrd_stats_dimension_set(st, "major", pgmajfault);
5848 // ----------------------------------------------------------------------------
5851 void *proc_main(void *ptr)
5855 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
5856 error("Cannot set pthread cancel type to DEFERRED.");
5858 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
5859 error("Cannot set pthread cancel state to ENABLE.");
5861 struct rusage me, me_last;
5862 struct timeval last, now;
5864 gettimeofday(&last, NULL);
5865 last.tv_sec -= update_every;
5867 // disable (by default) various interface that are not needed
5868 config_get_boolean("plugin:proc:/proc/net/dev", "interface lo", 0);
5869 config_get_boolean("plugin:proc:/proc/net/dev", "interface fireqos_monitor", 0);
5871 // when ZERO, attempt to do it
5872 int vdo_proc_net_dev = !config_get_boolean("plugin:proc", "/proc/net/dev", 1);
5873 int vdo_proc_diskstats = !config_get_boolean("plugin:proc", "/proc/diskstats", 1);
5874 int vdo_proc_net_snmp = !config_get_boolean("plugin:proc", "/proc/net/snmp", 1);
5875 int vdo_proc_net_netstat = !config_get_boolean("plugin:proc", "/proc/net/netstat", 1);
5876 int vdo_proc_net_stat_conntrack = !config_get_boolean("plugin:proc", "/proc/net/stat/conntrack", 1);
5877 int vdo_proc_net_ip_vs_stats = !config_get_boolean("plugin:proc", "/proc/net/ip_vs/stats", 1);
5878 int vdo_proc_stat = !config_get_boolean("plugin:proc", "/proc/stat", 1);
5879 int vdo_proc_meminfo = !config_get_boolean("plugin:proc", "/proc/meminfo", 1);
5880 int vdo_proc_vmstat = !config_get_boolean("plugin:proc", "/proc/vmstat", 1);
5881 int vdo_cpu_netdata = !config_get_boolean("plugin:proc", "netdata server resources", 1);
5883 RRD_STATS *stcpu = NULL, *stclients = NULL, *streqs = NULL, *stbytes = NULL;
5885 gettimeofday(&last, NULL);
5886 getrusage(RUSAGE_SELF, &me_last);
5888 unsigned long long usec = 0, susec = 0;
5891 // BEGIN -- the job to be done
5892 if(!vdo_proc_net_dev) vdo_proc_net_dev = do_proc_net_dev(usec);
5893 if(!vdo_proc_diskstats) vdo_proc_diskstats = do_proc_diskstats(usec);
5894 if(!vdo_proc_net_snmp) vdo_proc_net_snmp = do_proc_net_snmp(usec);
5895 if(!vdo_proc_net_netstat) vdo_proc_net_netstat = do_proc_net_netstat(usec);
5896 if(!vdo_proc_net_stat_conntrack) vdo_proc_net_stat_conntrack = do_proc_net_stat_conntrack(usec);
5897 if(!vdo_proc_net_ip_vs_stats) vdo_proc_net_ip_vs_stats = do_proc_net_ip_vs_stats(usec);
5898 if(!vdo_proc_stat) vdo_proc_stat = do_proc_stat(usec);
5899 if(!vdo_proc_meminfo) vdo_proc_meminfo = do_proc_meminfo(usec);
5900 if(!vdo_proc_vmstat) vdo_proc_vmstat = do_proc_vmstat(usec);
5901 // END -- the job is done
5903 // find the time to sleep in order to wait exactly update_every seconds
5904 gettimeofday(&now, NULL);
5905 usec = usecdiff(&now, &last) - susec;
5906 debug(D_PROCNETDEV_LOOP, "PROCNETDEV: last loop took %llu usec (worked for %llu, sleeped for %llu).", usec + susec, usec, susec);
5908 if(usec < (update_every * 1000000ULL / 2ULL)) susec = (update_every * 1000000ULL) - usec;
5909 else susec = update_every * 1000000ULL / 2ULL;
5911 // --------------------------------------------------------------------
5913 if(!vdo_cpu_netdata && getrusage(RUSAGE_SELF, &me) == 0) {
5915 unsigned long long cpuuser = me.ru_utime.tv_sec * 1000000ULL + me.ru_utime.tv_usec;
5916 unsigned long long cpusyst = me.ru_stime.tv_sec * 1000000ULL + me.ru_stime.tv_usec;
5918 if(!stcpu) stcpu = rrd_stats_find("netdata.server_cpu");
5920 stcpu = rrd_stats_create("netdata", "server_cpu", NULL, "netdata", "NetData CPU usage", "milliseconds/s", 9999, update_every, CHART_TYPE_STACKED);
5922 rrd_stats_dimension_add(stcpu, "user", NULL, 1, 1000, RRD_DIMENSION_INCREMENTAL);
5923 rrd_stats_dimension_add(stcpu, "system", NULL, 1, 1000, RRD_DIMENSION_INCREMENTAL);
5925 else rrd_stats_next(stcpu);
5927 rrd_stats_dimension_set(stcpu, "user", cpuuser);
5928 rrd_stats_dimension_set(stcpu, "system", cpusyst);
5929 rrd_stats_done(stcpu);
5931 bcopy(&me, &me_last, sizeof(struct rusage));
5933 // ----------------------------------------------------------------
5935 if(!stclients) stclients = rrd_stats_find("netdata.clients");
5937 stclients = rrd_stats_create("netdata", "clients", NULL, "netdata", "NetData Web Clients", "connected clients", 11000, update_every, CHART_TYPE_LINE);
5939 rrd_stats_dimension_add(stclients, "clients", NULL, 1, 1, RRD_DIMENSION_ABSOLUTE_NO_INTERPOLATION);
5941 else rrd_stats_next(stclients);
5943 rrd_stats_dimension_set(stclients, "clients", global_statistics.connected_clients);
5944 rrd_stats_done(stclients);
5946 // ----------------------------------------------------------------
5948 if(!streqs) streqs = rrd_stats_find("netdata.requests");
5950 streqs = rrd_stats_create("netdata", "requests", NULL, "netdata", "NetData Web Requests", "requests/s", 12000, update_every, CHART_TYPE_LINE);
5952 rrd_stats_dimension_add(streqs, "requests", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL_NO_INTERPOLATION);
5954 else rrd_stats_next(streqs);
5956 rrd_stats_dimension_set(streqs, "requests", global_statistics.web_requests);
5957 rrd_stats_done(streqs);
5959 // ----------------------------------------------------------------
5961 if(!stbytes) stbytes = rrd_stats_find("netdata.net");
5963 stbytes = rrd_stats_create("netdata", "net", NULL, "netdata", "NetData Network Traffic", "kilobits/s", 13000, update_every, CHART_TYPE_AREA);
5965 rrd_stats_dimension_add(stbytes, "in", NULL, 8, 1024, RRD_DIMENSION_INCREMENTAL);
5966 rrd_stats_dimension_add(stbytes, "out", NULL, -8, 1024, RRD_DIMENSION_INCREMENTAL);
5968 else rrd_stats_next(stbytes);
5970 rrd_stats_dimension_set(stbytes, "in", global_statistics.bytes_received);
5971 rrd_stats_dimension_set(stbytes, "out", global_statistics.bytes_sent);
5972 rrd_stats_done(stbytes);
5977 // copy current to last
5978 bcopy(&now, &last, sizeof(struct timeval));
5984 // ----------------------------------------------------------------------------
5985 // /sbin/tc processor
5986 // this requires the script plugins.d/tc-qos-helper.sh
5988 #define TC_LINE_MAX 1024
5991 char id[RRD_STATS_NAME_MAX + 1];
5992 char name[RRD_STATS_NAME_MAX + 1];
5994 char leafid[RRD_STATS_NAME_MAX + 1];
5995 char parentid[RRD_STATS_NAME_MAX + 1];
5999 unsigned long long bytes;
6001 struct tc_class *next;
6005 char id[RRD_STATS_NAME_MAX + 1];
6006 char name[RRD_STATS_NAME_MAX + 1];
6007 char family[RRD_STATS_NAME_MAX + 1];
6009 struct tc_class *classes;
6012 void tc_device_commit(struct tc_device *d)
6014 static int enable_new_interfaces = -1;
6016 if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean("plugin:tc", "enable new interfaces detected at runtime", 1);
6018 // we only need to add leaf classes
6019 struct tc_class *c, *x;
6021 for ( c = d->classes ; c ; c = c->next)
6024 for ( c = d->classes ; c ; c = c->next) {
6025 for ( x = d->classes ; x ; x = x->next) {
6026 if(x->parentid[0] && (strcmp(c->id, x->parentid) == 0 || strcmp(c->leafid, x->parentid) == 0)) {
6027 // debug(D_TC_LOOP, "TC: In device '%s', class '%s' (leafid: '%s') has leaf the class '%s' (parentid: '%s').", d->name, c->name, c->leafid, x->name, x->parentid);
6036 for ( c = d->classes ; c ; c = c->next) {
6037 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
6038 else debug(D_TC_LOOP, "TC: Device %s, class %s, IGNORE (isleaf: %d, hasparent: %d, parent: %s)", d->name, c->id, c->isleaf, c->hasparent, c->parentid);
6042 for ( c = d->classes ; c ; c = c->next) {
6043 if(c->isleaf && c->hasparent) break;
6046 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name);
6050 char var_name[4096 + 1];
6051 snprintf(var_name, 4096, "qos for %s", d->id);
6052 if(config_get_boolean("plugin:tc", var_name, enable_new_interfaces)) {
6053 RRD_STATS *st = rrd_stats_find_bytype(RRD_TYPE_TC, d->id);
6055 debug(D_TC_LOOP, "TC: Committing new TC device '%s'", d->name);
6057 st = rrd_stats_create(RRD_TYPE_TC, d->id, d->name, d->family, "Class Usage", "kilobits/s", 1000, update_every, CHART_TYPE_STACKED);
6059 for ( c = d->classes ; c ; c = c->next) {
6060 if(c->isleaf && c->hasparent)
6061 rrd_stats_dimension_add(st, c->id, c->name, 8, 1024, RRD_DIMENSION_INCREMENTAL);
6065 rrd_stats_next_plugins(st);
6067 if(strcmp(d->id, d->name) != 0) rrd_stats_set_name(st, d->name);
6070 for ( c = d->classes ; c ; c = c->next) {
6071 if(c->isleaf && c->hasparent) {
6072 if(rrd_stats_dimension_set(st, c->id, c->bytes) != 0) {
6074 // new class, we have to add it
6075 rrd_stats_dimension_add(st, c->id, c->name, 8, 1024, RRD_DIMENSION_INCREMENTAL);
6076 rrd_stats_dimension_set(st, c->id, c->bytes);
6079 // if it has a name, different to the id
6080 if(strcmp(c->id, c->name) != 0) {
6081 // update the rrd dimension with the new name
6083 for(rd = st->dimensions ; rd ; rd = rd->next) {
6084 if(strcmp(rd->id, c->id) == 0) { rrd_stats_dimension_set_name(st, rd, c->name); break; }
6093 void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
6096 for ( c = d->classes ; c ; c = c->next) {
6097 if(strcmp(c->id, id) == 0) {
6098 strncpy(c->name, name, RRD_STATS_NAME_MAX);
6099 // no need for null termination - it is already null
6105 void tc_device_set_device_name(struct tc_device *d, char *name)
6107 strncpy(d->name, name, RRD_STATS_NAME_MAX);
6108 // no need for null termination - it is already null
6111 void tc_device_set_device_family(struct tc_device *d, char *name)
6113 strncpy(d->family, name, RRD_STATS_NAME_MAX);
6114 // no need for null termination - it is already null
6117 struct tc_device *tc_device_create(char *name)
6119 struct tc_device *d;
6121 d = calloc(1, sizeof(struct tc_device));
6123 fatal("Cannot allocate memory for tc_device %s", name);
6127 strncpy(d->id, name, RRD_STATS_NAME_MAX);
6128 strcpy(d->name, d->id);
6129 strcpy(d->family, d->id);
6131 // no need for null termination on the strings, because of calloc()
6136 struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
6140 c = calloc(1, sizeof(struct tc_class));
6142 fatal("Cannot allocate memory for tc class");
6146 c->next = n->classes;
6149 strncpy(c->id, id, RRD_STATS_NAME_MAX);
6150 strcpy(c->name, c->id);
6151 if(parentid) strncpy(c->parentid, parentid, RRD_STATS_NAME_MAX);
6152 if(leafid) strncpy(c->leafid, leafid, RRD_STATS_NAME_MAX);
6154 // no need for null termination on the strings, because of calloc()
6159 void tc_class_free(struct tc_class *c)
6161 if(c->next) tc_class_free(c->next);
6165 void tc_device_free(struct tc_device *n)
6167 if(n->classes) tc_class_free(n->classes);
6171 pid_t tc_child_pid = 0;
6172 void *tc_main(void *ptr)
6176 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
6177 error("Cannot set pthread cancel type to DEFERRED.");
6179 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
6180 error("Cannot set pthread cancel state to ENABLE.");
6182 char buffer[TC_LINE_MAX+1] = "";
6186 struct tc_device *device = NULL;
6187 struct tc_class *class = NULL;
6189 snprintf(buffer, TC_LINE_MAX, "exec %s %d", config_get("plugin:tc", "script to run to get tc values", PLUGINS_DIR "/tc-qos-helper.sh"), update_every);
6190 debug(D_TC_LOOP, "executing '%s'", buffer);
6191 // fp = popen(buffer, "r");
6192 fp = mypopen(buffer, &tc_child_pid);
6194 error("TC: Cannot popen(\"%s\", \"r\").", buffer);
6198 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
6199 buffer[TC_LINE_MAX] = '\0';
6200 char *b = buffer, *p;
6201 // debug(D_TC_LOOP, "TC: read '%s'", buffer);
6203 p = strsep(&b, " \n");
6204 while (p && (*p == ' ' || *p == '\0')) p = strsep(&b, " \n");
6207 if(strcmp(p, "END") == 0) {
6209 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
6210 error("Cannot set pthread cancel state to DISABLE.");
6212 tc_device_commit(device);
6213 tc_device_free(device);
6217 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
6218 error("Cannot set pthread cancel state to ENABLE.");
6221 else if(strcmp(p, "BEGIN") == 0) {
6223 tc_device_free(device);
6228 p = strsep(&b, " \n");
6230 device = tc_device_create(p);
6234 else if(device && (strcmp(p, "class") == 0)) {
6235 p = strsep(&b, " \n"); // the class: htb, fq_codel, etc
6236 char *id = strsep(&b, " \n"); // the class major:minor
6237 char *parent = strsep(&b, " \n"); // 'parent' or 'root'
6238 char *parentid = strsep(&b, " \n"); // the parent's id
6239 char *leaf = strsep(&b, " \n"); // 'leaf'
6240 char *leafid = strsep(&b, " \n"); // leafid
6243 && parent && *parent
6244 && parentid && *parentid
6246 (strcmp(parent, "parent") == 0 && parentid && *parentid)
6247 || strcmp(parent, "root") == 0
6250 if(strcmp(parent, "root") == 0) {
6254 else if(!leaf || strcmp(leaf, "leaf") != 0)
6257 char leafbuf[20 + 1] = "";
6258 if(leafid && leafid[strlen(leafid) - 1] == ':') {
6259 strncpy(leafbuf, leafid, 20 - 1);
6260 strcat(leafbuf, "1");
6264 class = tc_class_add(device, id, parentid, leafid);
6267 else if(device && class && (strcmp(p, "Sent") == 0)) {
6268 p = strsep(&b, " \n");
6269 if(p && *p) class->bytes = atoll(p);
6271 else if(device && (strcmp(p, "SETDEVICENAME") == 0)) {
6272 char *name = strsep(&b, " \n");
6273 if(name && *name) tc_device_set_device_name(device, name);
6275 else if(device && (strcmp(p, "SETDEVICEGROUP") == 0)) {
6276 char *name = strsep(&b, " \n");
6277 if(name && *name) tc_device_set_device_family(device, name);
6279 else if(device && (strcmp(p, "SETCLASSNAME") == 0)) {
6280 char *id = strsep(&b, " \n");
6281 char *path = strsep(&b, " \n");
6282 if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
6284 #ifdef DETACH_PLUGINS_FROM_NETDATA
6285 else if((strcmp(p, "MYPID") == 0)) {
6286 char *id = strsep(&b, " \n");
6287 pid_t pid = atol(id);
6289 if(pid) tc_child_pid = pid;
6291 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
6298 tc_device_free(device);
6303 sleep(update_every);
6309 // ----------------------------------------------------------------------------
6310 // cpu jitter calculation
6312 #define CPU_IDLEJITTER_SLEEP_TIME_MS 20
6314 void *cpuidlejitter_main(void *ptr)
6318 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
6319 error("Cannot set pthread cancel type to DEFERRED.");
6321 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
6322 error("Cannot set pthread cancel state to ENABLE.");
6324 int sleep_ms = config_get_number("plugin:idlejitter", "loop time in ms", CPU_IDLEJITTER_SLEEP_TIME_MS);
6326 config_set_number("plugin:idlejitter", "loop time in ms", CPU_IDLEJITTER_SLEEP_TIME_MS);
6327 sleep_ms = CPU_IDLEJITTER_SLEEP_TIME_MS;
6330 RRD_STATS *st = rrd_stats_find("system.idlejitter");
6332 st = rrd_stats_create("system", "idlejitter", NULL, "cpu", "CPU Idle Jitter", "microseconds lost/s", 9999, update_every, CHART_TYPE_LINE);
6333 rrd_stats_dimension_add(st, "jitter", NULL, 1, 1, RRD_DIMENSION_ABSOLUTE);
6336 struct timeval before, after;
6337 unsigned long long counter;
6338 for(counter = 0; 1 ;counter++) {
6339 unsigned long long usec = 0, susec = 0;
6341 while(susec < (update_every * 1000000ULL)) {
6343 gettimeofday(&before, NULL);
6344 usleep(sleep_ms * 1000);
6345 gettimeofday(&after, NULL);
6347 // calculate the time it took for a full loop
6348 usec = usecdiff(&after, &before);
6351 usec -= (sleep_ms * 1000);
6353 if(counter) rrd_stats_next_usec(st, susec);
6354 rrd_stats_dimension_set(st, "jitter", usec);
6361 // ----------------------------------------------------------------------------
6364 void *checks_main(void *ptr)
6368 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
6369 error("Cannot set pthread cancel type to DEFERRED.");
6371 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
6372 error("Cannot set pthread cancel state to ENABLE.");
6374 unsigned long long usec = 0, susec = update_every * 1000000ULL, loop_usec = 0, total_susec = 0;
6375 struct timeval now, last, loop;
6377 RRD_STATS *check1, *check2, *check3, *apps_cpu = NULL;
6379 check1 = rrd_stats_create("netdata", "check1", NULL, "netdata", "Caller gives microseconds", "a million !", 99999, update_every, CHART_TYPE_LINE);
6380 rrd_stats_dimension_add(check1, "absolute", NULL, -1, 1, RRD_DIMENSION_ABSOLUTE);
6381 rrd_stats_dimension_add(check1, "incremental", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
6383 check2 = rrd_stats_create("netdata", "check2", NULL, "netdata", "Netdata calcs microseconds", "a million !", 99999, update_every, CHART_TYPE_LINE);
6384 rrd_stats_dimension_add(check2, "absolute", NULL, -1, 1, RRD_DIMENSION_ABSOLUTE);
6385 rrd_stats_dimension_add(check2, "incremental", NULL, 1, 1, RRD_DIMENSION_INCREMENTAL);
6387 check3 = rrd_stats_create("netdata", "checkdt", NULL, "netdata", "Clock difference", "microseconds diff", 99999, update_every, CHART_TYPE_LINE);
6388 rrd_stats_dimension_add(check3, "caller", NULL, 1, 1, RRD_DIMENSION_ABSOLUTE);
6389 rrd_stats_dimension_add(check3, "netdata", NULL, 1, 1, RRD_DIMENSION_ABSOLUTE);
6390 rrd_stats_dimension_add(check3, "apps.plugin", NULL, 1, 1, RRD_DIMENSION_ABSOLUTE);
6392 gettimeofday(&last, NULL);
6396 // find the time to sleep in order to wait exactly update_every seconds
6397 gettimeofday(&now, NULL);
6398 loop_usec = usecdiff(&now, &last);
6399 usec = loop_usec - susec;
6400 debug(D_PROCNETDEV_LOOP, "CHECK: last loop took %llu usec (worked for %llu, sleeped for %llu).", loop_usec, usec, susec);
6402 if(usec < (update_every * 1000000ULL / 2ULL)) susec = (update_every * 1000000ULL) - usec;
6403 else susec = update_every * 1000000ULL / 2ULL;
6405 // --------------------------------------------------------------------
6406 // Calculate loop time
6408 last.tv_sec = now.tv_sec;
6409 last.tv_usec = now.tv_usec;
6410 total_susec += loop_usec;
6412 // --------------------------------------------------------------------
6415 if(check1->counter_done) rrd_stats_next_usec(check1, loop_usec);
6416 rrd_stats_dimension_set(check1, "absolute", 1000000);
6417 rrd_stats_dimension_set(check1, "incremental", total_susec);
6418 rrd_stats_done(check1);
6420 // --------------------------------------------------------------------
6423 if(check2->counter_done) rrd_stats_next(check2);
6424 rrd_stats_dimension_set(check2, "absolute", 1000000);
6425 rrd_stats_dimension_set(check2, "incremental", total_susec);
6426 rrd_stats_done(check2);
6428 // --------------------------------------------------------------------
6431 if(!apps_cpu) apps_cpu = rrd_stats_find("apps.cpu");
6432 if(check3->counter_done) rrd_stats_next_usec(check3, loop_usec);
6433 gettimeofday(&loop, NULL);
6434 rrd_stats_dimension_set(check3, "caller", (long long)usecdiff(&loop, &check1->last_collected));
6435 rrd_stats_dimension_set(check3, "netdata", (long long)usecdiff(&loop, &check2->last_collected));
6436 if(apps_cpu) rrd_stats_dimension_set(check3, "apps.plugin", (long long)usecdiff(&loop, &apps_cpu->last_collected));
6437 rrd_stats_done(check3);
6443 // ----------------------------------------------------------------------------
6446 #define PLUGINSD_FILE_SUFFIX ".plugin"
6447 #define PLUGINSD_FILE_SUFFIX_LEN strlen(PLUGINSD_FILE_SUFFIX)
6448 #define PLUGINSD_CMD_MAX (FILENAME_MAX*2)
6449 #define PLUGINSD_LINE_MAX 1024
6452 char id[CONFIG_MAX_NAME+1]; // config node id
6454 char filename[FILENAME_MAX+1]; // just the filename
6455 char fullfilename[FILENAME_MAX+1]; // with path
6456 char cmd[PLUGINSD_CMD_MAX+1]; // the command that is executes
6467 struct plugind *next;
6468 } *pluginsd_root = NULL;
6470 // like strsep() but:
6471 // it trims spaces before and after each value
6472 // it accepts quoted values in single or double quotes
6473 char *qstrsep(char **ptr)
6475 if(!*ptr || !**ptr) return NULL;
6479 // skip leading spaces
6480 while(isspace(*p)) p++;
6482 // if the first char is a quote, assume quoted
6483 if(*p == '"' || *p == '\'') {
6486 while(*p && *p != q) p++;
6498 while(*p && !isspace(*p)) p++;
6499 if(!*p) *ptr = NULL;
6508 void *pluginsd_worker_thread(void *arg)
6510 struct plugind *cd = (struct plugind *)arg;
6511 char line[PLUGINSD_LINE_MAX + 1];
6513 #ifdef DETACH_PLUGINS_FROM_NETDATA
6514 unsigned long long usec = 0, susec = 0;
6515 struct timeval last = {0, 0} , now = {0, 0};
6519 FILE *fp = mypopen(cd->cmd, &cd->pid);
6521 error("Cannot popen(\"%s\", \"r\").", cd->cmd);
6525 RRD_STATS *st = NULL;
6527 unsigned long long count = 0;
6528 while(fgets(line, PLUGINSD_LINE_MAX, fp) != NULL) {
6529 char *p = trim(line);
6530 debug(D_PLUGINSD, "PLUGINSD: %s: %s", cd->filename, line);
6532 char *s = qstrsep(&p);
6534 if(!s || !*s) continue;
6535 else if(!strcmp(s, "SET")) {
6537 while((t = strchr(p, '='))) *t = ' ';
6539 char *dimension = qstrsep(&p);
6540 char *value = qstrsep(&p);
6542 if(!dimension || !*dimension || !value) {
6543 error("PLUGINSD: '%s' is requesting a SET on chart '%s', like this: 'SET %s = %s'. Disabling it.", cd->fullfilename, st->id, dimension?dimension:"", value?value:"");
6545 kill(cd->pid, SIGTERM);
6550 error("PLUGINSD: '%s' is requesting a SET on dimension %s with value %s, without a BEGIN. Disabling it.", cd->fullfilename, dimension, value);
6552 kill(cd->pid, SIGTERM);
6556 if(st->debug) debug(D_PLUGINSD, "PLUGINSD: '%s' is setting dimension %s/%s to %s", cd->fullfilename, st->id, dimension, value);
6557 rrd_stats_dimension_set(st, dimension, atoll(value));
6561 else if(!strcmp(s, "BEGIN")) {
6562 char *id = qstrsep(&p);
6563 char *microseconds_txt = qstrsep(&p);
6566 error("PLUGINSD: '%s' is requesting a BEGIN without a chart id. Disabling it.", cd->fullfilename);
6568 kill(cd->pid, SIGTERM);
6572 st = rrd_stats_find(id);
6574 error("PLUGINSD: '%s' is requesting a BEGIN on chart '%s', which does not exist. Disabling it.", cd->fullfilename, id);
6576 kill(cd->pid, SIGTERM);
6580 if(st->counter_done) {
6581 unsigned long long microseconds = 0;
6582 if(microseconds_txt && *microseconds_txt) microseconds = strtoull(microseconds_txt, NULL, 10);
6583 if(microseconds) rrd_stats_next_usec(st, microseconds);
6584 else rrd_stats_next_plugins(st);
6587 else if(!strcmp(s, "END")) {
6589 error("PLUGINSD: '%s' is requesting an END, without a BEGIN. Disabling it.", cd->fullfilename);
6591 kill(cd->pid, SIGTERM);
6595 if(st->debug) debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a END on chart %s", cd->fullfilename, st->id);
6600 else if(!strcmp(s, "FLUSH")) {
6601 debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a FLUSH", cd->fullfilename);
6604 else if(!strcmp(s, "CHART")) {
6607 char *type = qstrsep(&p);
6610 id = strchr(type, '.');
6611 if(id) { *id = '\0'; id++; }
6613 char *name = qstrsep(&p);
6614 char *title = qstrsep(&p);
6615 char *units = qstrsep(&p);
6616 char *family = qstrsep(&p);
6617 char *category = qstrsep(&p);
6618 char *chart = qstrsep(&p);
6619 char *priority_s = qstrsep(&p);
6620 char *update_every_s = qstrsep(&p);
6622 if(!type || !*type || !id || !*id) {
6623 error("PLUGINSD: '%s' is requesting a CHART, without a type.id. Disabling it.", cd->fullfilename);
6625 kill(cd->pid, SIGTERM);
6629 int priority = 1000;
6630 if(priority_s) priority = atoi(priority_s);
6632 int update_every = cd->update_every;
6633 if(update_every_s) update_every = atoi(update_every_s);
6634 if(!update_every) update_every = cd->update_every;
6636 int chart_type = CHART_TYPE_LINE;
6637 if(chart) chart_type = chart_type_id(chart);
6639 if(!name || !*name) name = NULL;
6640 if(!family || !*family) family = id;
6641 if(!category || !*category) category = type;
6643 st = rrd_stats_find_bytype(type, id);
6645 debug(D_PLUGINSD, "PLUGINSD: Creating chart type='%s', id='%s', name='%s', family='%s', category='%s', chart='%s', priority=%d, update_every=%d"
6649 , category?category:""
6650 , chart_type_name(chart_type)
6655 st = rrd_stats_create(type, id, name, family, title, units, priority, update_every, chart_type);
6656 cd->update_every = update_every;
6658 if(strcmp(category, "none") == 0) st->isdetail = 1;
6660 else debug(D_PLUGINSD, "PLUGINSD: Chart '%s' already exists. Not adding it again.", st->id);
6662 else if(!strcmp(s, "DIMENSION")) {
6663 char *id = qstrsep(&p);
6664 char *name = qstrsep(&p);
6665 char *algorithm = qstrsep(&p);
6666 char *multiplier_s = qstrsep(&p);
6667 char *divisor_s = qstrsep(&p);
6668 char *hidden = qstrsep(&p);
6671 error("PLUGINSD: '%s' is requesting a DIMENSION, without an id. Disabling it.", cd->fullfilename);
6673 kill(cd->pid, SIGTERM);
6678 error("PLUGINSD: '%s' is requesting a DIMENSION, without a CHART. Disabling it.", cd->fullfilename);
6680 kill(cd->pid, SIGTERM);
6684 long multiplier = 1;
6685 if(multiplier_s && *multiplier_s) multiplier = atol(multiplier_s);
6686 if(!multiplier) multiplier = 1;
6689 if(divisor_s && *divisor_s) divisor = atol(divisor_s);
6690 if(!divisor) divisor = 1;
6692 if(!algorithm || !*algorithm) algorithm = "absolute";
6694 if(st->debug) debug(D_PLUGINSD, "PLUGINSD: Creating dimension in chart %s, id='%s', name='%s', algorithm='%s', multiplier=%ld, divisor=%ld, hidden='%s'"
6698 , algorithm_name(algorithm_id(algorithm))
6704 RRD_DIMENSION *rd = rrd_stats_dimension_find(st, id);
6706 rd = rrd_stats_dimension_add(st, id, name, multiplier, divisor, algorithm_id(algorithm));
6707 if(hidden && strcmp(hidden, "hidden") == 0) rd->hidden = 1;
6709 else if(st->debug) debug(D_PLUGINSD, "PLUGINSD: dimension %s/%s already exists. Not adding it again.", st->id, id);
6711 else if(!strcmp(s, "DISABLE")) {
6712 error("PLUGINSD: '%s' called DISABLE. Disabling it.", cd->fullfilename);
6714 kill(cd->pid, SIGTERM);
6717 #ifdef DETACH_PLUGINS_FROM_NETDATA
6718 else if(!strcmp(s, "MYPID")) {
6719 char *pid_s = qstrsep(&p);
6720 pid_t pid = atol(pid_s);
6722 if(pid) cd->pid = pid;
6723 debug(D_PLUGINSD, "PLUGINSD: %s is on pid %d", cd->id, cd->pid);
6725 else if(!strcmp(s, "STOPPING_WAKE_ME_UP_PLEASE")) {
6726 error("PLUGINSD: '%s' (pid %d) called STOPPING_WAKE_ME_UP_PLEASE.", cd->fullfilename, cd->pid);
6728 gettimeofday(&now, NULL);
6729 if(!usec && !susec) {
6731 susec = cd->update_every * 1000000ULL;
6735 usec = usecdiff(&now, &last) - susec;
6736 error("PLUGINSD: %s last loop took %llu usec (worked for %llu, sleeped for %llu).\n", cd->fullfilename, usec + susec, usec, susec);
6737 if(usec < (update_every * 1000000ULL / 2ULL)) susec = (update_every * 1000000ULL) - usec;
6738 else susec = update_every * 1000000ULL / 2ULL;
6741 error("PLUGINSD: %s sleeping for %llu. Will kill with SIGCONT pid %d to wake it up.\n", cd->fullfilename, susec, cd->pid);
6743 kill(cd->pid, SIGCONT);
6744 bcopy(&now, &last, sizeof(struct timeval));
6749 error("PLUGINSD: '%s' is sending command '%s' which is not known by netdata. Disabling it.", cd->fullfilename, s);
6751 kill(cd->pid, SIGTERM);
6756 // fgets() failed or loop broke
6759 if(!count && cd->enabled) {
6760 error("PLUGINSD: '%s' does not generate usefull output. Disabling it.", cd->fullfilename);
6762 kill(cd->pid, SIGTERM);
6765 if(cd->enabled) sleep(cd->update_every);
6773 void *pluginsd_main(void *ptr)
6777 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
6778 error("Cannot set pthread cancel type to DEFERRED.");
6780 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
6781 error("Cannot set pthread cancel state to ENABLE.");
6783 char *dir_name = config_get("plugins", "plugins directory", PLUGINS_DIR);
6784 int automatic_run = config_get_boolean("plugins", "enable running new plugins", 0);
6785 int scan_frequency = config_get_number("plugins", "check for new plugins every", 60);
6787 struct dirent *file = NULL;
6790 // enable the apps plugin by default
6791 config_get_boolean("plugins", "apps", 1);
6793 if(scan_frequency < 1) scan_frequency = 1;
6796 dir = opendir(dir_name);
6798 error("Cannot open directory '%s'.", dir_name);
6802 while((file = readdir(dir))) {
6803 debug(D_PLUGINSD, "PLUGINSD: Examining file '%s'", file->d_name);
6805 if(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0) continue;
6807 int len = strlen(file->d_name);
6808 if(len <= (int)PLUGINSD_FILE_SUFFIX_LEN) continue;
6809 if(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - PLUGINSD_FILE_SUFFIX_LEN]) != 0) {
6810 debug(D_PLUGINSD, "PLUGINSD: File '%s' does not end in '%s'.", file->d_name, PLUGINSD_FILE_SUFFIX);
6814 char pluginname[CONFIG_MAX_NAME + 1];
6815 snprintf(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
6816 int enabled = config_get_boolean("plugins", pluginname, automatic_run);
6819 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is not enabled", file->d_name);
6823 // check if it runs already
6824 for(cd = pluginsd_root ; cd ; cd = cd->next) {
6825 if(strcmp(cd->filename, file->d_name) == 0) break;
6827 if(cd && !cd->obsolete) {
6828 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is already running", cd->filename);
6832 // it is not running
6833 // allocate a new one, or use the obsolete one
6835 cd = calloc(sizeof(struct plugind), 1);
6836 if(!cd) fatal("Cannot allocate memory for plugin.");
6838 snprintf(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
6840 strncpy(cd->filename, file->d_name, FILENAME_MAX);
6841 snprintf(cd->fullfilename, FILENAME_MAX, "%s/%s", dir_name, cd->filename);
6843 cd->enabled = enabled;
6844 cd->update_every = config_get_number(cd->id, "update every", update_every);
6845 cd->started_t = time(NULL);
6848 snprintf(cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every, config_get(cd->id, "command options", def));
6851 if(pluginsd_root) cd->next = pluginsd_root;
6856 if(!cd->enabled) continue;
6858 // spawn a new thread for it
6859 if(pthread_create(&cd->thread, NULL, pluginsd_worker_thread, cd) != 0) {
6860 error("CHARTS.D: failed to create new thread for chart.d %s.", cd->filename);
6863 else if(pthread_detach(cd->thread) != 0)
6864 error("CHARTS.D: Cannot request detach of newly created thread for chart.d %s.", cd->filename);
6868 sleep(scan_frequency);
6875 // ----------------------------------------------------------------------------
6876 // main and related functions
6878 pthread_t *p_proc = NULL, *p_tc = NULL, *p_jitter = NULL, *p_pluginsd = NULL, *p_checks = NULL;
6884 struct web_client *w;
6885 for(w = web_clients; w ; w = w->next) {
6886 debug(D_EXIT, "Stopping web client %s", w->client_ip);
6887 pthread_cancel(w->thread);
6888 pthread_join(w->thread, NULL);
6892 debug(D_EXIT, "Stopping proc thread");
6893 pthread_cancel(*p_proc);
6894 pthread_join(*p_proc, NULL);
6899 debug(D_EXIT, "Stopping idlejitter thread");
6900 pthread_cancel(*p_jitter);
6901 pthread_join(*p_jitter, NULL);
6906 debug(D_EXIT, "Stopping self-checks thread");
6907 pthread_cancel(*p_checks);
6908 pthread_join(*p_checks, NULL);
6914 debug(D_EXIT, "Killing tc-qos-helper procees");
6915 kill(tc_child_pid, SIGTERM);
6916 waitid(tc_child_pid, 0, &info, WEXITED);
6920 debug(D_EXIT, "Stopping tc plugin thread");
6921 pthread_cancel(*p_tc);
6922 pthread_join(*p_tc, NULL);
6928 for(cd = pluginsd_root ; cd ; cd = cd->next) {
6929 debug(D_EXIT, "Stopping %s plugin thread", cd->id);
6930 pthread_cancel(cd->thread);
6931 pthread_join(cd->thread, NULL);
6933 if(cd->pid && !cd->obsolete) {
6934 debug(D_EXIT, "killing %s plugin process", cd->id);
6935 kill(cd->pid, SIGTERM);
6936 waitid(cd->pid, 0, &info, WEXITED);
6940 debug(D_EXIT, "Stopping plugin manager thread");
6941 pthread_cancel(*p_pluginsd);
6942 pthread_join(*p_pluginsd, NULL);
6946 debug(D_EXIT, "All threads/childs stopped.");
6949 void sig_handler(int signo)
6960 debug(D_EXIT, "Signaled exit (signal %d). Errno: %d (%s)", signo, errno, strerror(errno));
6961 signal(SIGCHLD, SIG_IGN);
6962 signal(SIGPIPE, SIG_IGN);
6963 signal(SIGTERM, SIG_IGN);
6964 signal(SIGQUIT, SIG_IGN);
6965 signal(SIGHUP, SIG_IGN);
6966 signal(SIGINT, SIG_IGN);
6968 rrd_stats_free_all();
6969 debug(D_EXIT, "Waiting for any child exits...");
6970 while(waitid(P_ALL, 0, &info, WEXITED|WNOHANG) == 0) if(!info.si_pid) break;
6971 //unlink("/var/run/netdata.pid");
6972 info("NetData exiting. Bye bye...");
6977 info("Ignoring signal %d. Errno: %d (%s)", signo, errno, strerror(errno));
6982 info("Received SIGCHLD (signal %d).", signo);
6983 while(waitid(P_ALL, 0, &info, WEXITED|WNOHANG) == 0) {
6984 if(!info.si_pid) break;
6985 switch(info.si_code) {
6987 error("pid %d exited with code %d.", info.si_pid, info.si_status);
6991 error("pid %d killed by signal %d.", info.si_pid, info.si_status);
6995 error("pid %d core dumped by signal %d.", info.si_pid, info.si_status);
6999 error("pid %d stopped by signal %d.", info.si_pid, info.si_status);
7003 error("pid %d trapped by signal %d.", info.si_pid, info.si_status);
7007 error("pid %d continued by signal %d.", info.si_pid, info.si_status);
7011 error("pid %d gave us a SIGCHLD with code %d and status %d.", info.si_pid, info.si_code, info.si_status);
7018 info("Signal %d received. Falling back to default action for it.", signo);
7019 signal(signo, SIG_DFL);
7024 int become_user(const char *username)
7026 struct passwd *pw = getpwnam(username);
7028 fprintf(stderr, "User %s is not present. Error: %s\n", username, strerror(errno));
7031 if(setgid(pw->pw_gid) != 0) {
7032 fprintf(stderr, "Cannot switch to user's %s group (gid: %d). Error: %s\n", username, pw->pw_gid, strerror(errno));
7035 if(setegid(pw->pw_gid) != 0) {
7036 fprintf(stderr, "Cannot effectively switch to user's %s group (gid: %d). Error: %s\n", username, pw->pw_gid, strerror(errno));
7039 if(setuid(pw->pw_uid) != 0) {
7040 fprintf(stderr, "Cannot switch to user %s (uid: %d). Error: %s\n", username, pw->pw_uid, strerror(errno));
7043 if(seteuid(pw->pw_uid) != 0) {
7044 fprintf(stderr, "Cannot effectively switch to user %s (uid: %d). Error: %s\n", username, pw->pw_uid, strerror(errno));
7051 int fd_is_valid(int fd)
7053 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
7056 int become_daemon(int close_all_files, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp)
7060 // open the files before forking
7061 int input_fd = -1, output_fd = -1, error_fd = -1, dev_null = -1;
7063 if(input && *input) {
7064 if((input_fd = open(input, O_RDONLY, 0666)) == -1) {
7065 fprintf(stderr, "Cannot open input file '%s' (%s).", input, strerror(errno));
7070 if(output && *output) {
7071 if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
7072 fprintf(stderr, "Cannot open output log file '%s' (%s).", output, strerror(errno));
7073 if(input_fd != -1) close(input_fd);
7078 if(error && *error) {
7079 if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
7080 fprintf(stderr, "Cannot open error log file '%s' (%s).", error, strerror(errno));
7081 if(input_fd != -1) close(input_fd);
7082 if(output_fd != -1) close(output_fd);
7087 if(access && *access && access_fd) {
7088 if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
7089 fprintf(stderr, "Cannot open access log file '%s' (%s).", access, strerror(errno));
7090 if(input_fd != -1) close(input_fd);
7091 if(output_fd != -1) close(output_fd);
7092 if(error_fd != -1) close(error_fd);
7097 *access_fp = fdopen(*access_fd, "w");
7099 fprintf(stderr, "Cannot migrate file's '%s' fd %d (%s).\n", access, *access_fd, strerror(errno));
7100 if(input_fd != -1) close(input_fd);
7101 if(output_fd != -1) close(output_fd);
7102 if(error_fd != -1) close(error_fd);
7110 if((dev_null = open("/dev/null", O_RDWR, 0666)) == -1) {
7111 perror("Cannot open /dev/null");
7112 if(input_fd != -1) close(input_fd);
7113 if(output_fd != -1) close(output_fd);
7114 if(error_fd != -1) close(error_fd);
7115 if(access && access_fd && *access_fd != -1) {
7131 perror("cannot fork");
7135 exit(0); // the parent
7138 // become session leader
7142 signal(SIGCHLD, SIG_IGN);
7143 signal(SIGHUP, SIG_IGN);
7144 signal(SIGWINCH, SIG_IGN);
7149 perror("cannot fork");
7153 exit(0); // the parent
7156 // Set new file permissions
7160 if(close_all_files) {
7161 for(i = sysconf(_SC_OPEN_MAX); i > 0; i--)
7163 ((access_fd && i != *access_fd) || !access_fd)
7172 close(STDIN_FILENO);
7173 close(STDOUT_FILENO);
7174 close(STDERR_FILENO);
7177 // put the opened files
7178 // to our standard file descriptors
7179 if(input_fd != -1) {
7180 if(input_fd != STDIN_FILENO) {
7181 dup2(input_fd, STDIN_FILENO);
7186 else dup2(dev_null, STDIN_FILENO);
7188 if(output_fd != -1) {
7189 if(output_fd != STDOUT_FILENO) {
7190 dup2(output_fd, STDOUT_FILENO);
7195 else dup2(dev_null, STDOUT_FILENO);
7197 if(error_fd != -1) {
7198 if(error_fd != STDERR_FILENO) {
7199 dup2(error_fd, STDERR_FILENO);
7204 else dup2(dev_null, STDERR_FILENO);
7207 if(dev_null != STDIN_FILENO && dev_null != STDOUT_FILENO && dev_null != STDERR_FILENO)
7210 /* // generate our pid file
7212 unlink("/var/run/netdata.pid");
7213 int fd = open("/var/run/netdata.pid", O_RDWR | O_CREAT, 0666);
7216 sprintf(b, "%d\n", getpid());
7217 write(fd, b, strlen(b));
7225 int main(int argc, char **argv)
7228 int config_loaded = 0;
7231 // parse the arguments
7232 for(i = 1; i < argc ; i++) {
7233 if(strcmp(argv[i], "-c") == 0 && (i+1) < argc) {
7234 if(load_config(argv[i+1], 1) != 1) {
7235 fprintf(stderr, "Cannot load configuration file %s. Reason: %s\n", argv[i+1], strerror(errno));
7239 debug(D_OPTIONS, "Configuration loaded from %s.", argv[i+1]);
7244 else if(strcmp(argv[i], "-df") == 0 && (i+1) < argc) { config_set("global", "debug flags", argv[i+1]); i++; }
7245 else if(strcmp(argv[i], "-p") == 0 && (i+1) < argc) { config_set("global", "port", argv[i+1]); i++; }
7246 else if(strcmp(argv[i], "-u") == 0 && (i+1) < argc) { config_set("global", "run as user", argv[i+1]); i++; }
7247 else if(strcmp(argv[i], "-l") == 0 && (i+1) < argc) { config_set("global", "history", argv[i+1]); i++; }
7248 else if(strcmp(argv[i], "-t") == 0 && (i+1) < argc) { config_set("global", "update every", argv[i+1]); i++; }
7250 fprintf(stderr, "Cannot understand option '%s'.\n", argv[i]);
7251 fprintf(stderr, "\nUSAGE: %s [-d] [-l LINES_TO_SAVE] [-u UPDATE_TIMER] [-p LISTEN_PORT] [-dl debug log file] [-df debug flags].\n\n", argv[0]);
7252 fprintf(stderr, " -c CONFIG FILE the configuration file to load. Default: %s.\n", CONFIG_DIR "/" CONFIG_FILENAME);
7253 fprintf(stderr, " -l LINES_TO_SAVE can be from 5 to %d lines in JSON data. Default: %d.\n", HISTORY_MAX, HISTORY);
7254 fprintf(stderr, " -t UPDATE_TIMER can be from 1 to %d seconds. Default: %d.\n", UPDATE_EVERY_MAX, UPDATE_EVERY);
7255 fprintf(stderr, " -p LISTEN_PORT can be from 1 to %d. Default: %d.\n", 65535, LISTEN_PORT);
7256 fprintf(stderr, " -u USERNAME can be any system username to run as. Default: none.\n");
7257 fprintf(stderr, " -df FLAGS debug options. Default: 0x%8llx.\n", debug_flags);
7262 if(!config_loaded) load_config(NULL, 0);
7264 char *input_log_file = NULL;
7265 char *output_log_file = NULL;
7266 char *error_log_file = NULL;
7267 char *access_log_file = NULL;
7271 // --------------------------------------------------------------------
7273 sprintf(buffer, "0x%08llx", 0ULL);
7274 char *flags = config_get("global", "debug flags", buffer);
7275 debug_flags = strtoull(flags, NULL, 0);
7276 debug(D_OPTIONS, "Debug flags set to '0x%8llx'.", debug_flags);
7278 // --------------------------------------------------------------------
7280 output_log_file = config_get("global", "debug log", LOG_DIR "/debug.log");
7281 if(strcmp(output_log_file, "syslog") == 0) {
7282 output_log_syslog = 1;
7283 output_log_file = NULL;
7285 else if(strcmp(output_log_file, "none") == 0) {
7286 output_log_syslog = 0;
7287 output_log_file = NULL;
7289 else output_log_syslog = 0;
7291 // --------------------------------------------------------------------
7294 error_log_file = config_get("global", "error log", LOG_DIR "/error.log");
7295 if(strcmp(error_log_file, "syslog") == 0) {
7296 error_log_syslog = 1;
7297 error_log_file = NULL;
7299 else if(strcmp(error_log_file, "none") == 0) {
7300 error_log_syslog = 0;
7301 error_log_file = NULL;
7302 silent = 1; // optimization - do not even generate debug log entries
7304 else error_log_syslog = 0;
7306 // --------------------------------------------------------------------
7308 access_log_file = config_get("global", "access log", LOG_DIR "/access.log");
7309 if(strcmp(access_log_file, "syslog") == 0) {
7310 access_log_syslog = 1;
7311 access_log_file = NULL;
7313 else if(strcmp(access_log_file, "none") == 0) {
7314 access_log_syslog = 0;
7315 access_log_file = NULL;
7317 else access_log_syslog = 0;
7319 // --------------------------------------------------------------------
7321 memory_mode = memory_mode_id(config_get("global", "memory mode", memory_mode_name(memory_mode)));
7323 // --------------------------------------------------------------------
7325 if(gethostname(buffer, HOSTNAME_MAX) == -1)
7326 error("WARNING: Cannot get machine hostname.");
7327 hostname = config_get("global", "hostname", buffer);
7328 debug(D_OPTIONS, "hostname set to '%s'", hostname);
7330 // --------------------------------------------------------------------
7332 save_history = config_get_number("global", "history", HISTORY);
7333 if(save_history < 5 || save_history > HISTORY_MAX) {
7334 fprintf(stderr, "Invalid save lines %d given. Defaulting to %d.\n", save_history, HISTORY);
7335 save_history = HISTORY;
7338 debug(D_OPTIONS, "save lines set to %d.", save_history);
7341 // --------------------------------------------------------------------
7343 char *user = config_get("global", "run as user", (getuid() == 0)?"nobody":"");
7345 if(become_user(user) != 0) {
7346 fprintf(stderr, "Cannot become user %s.\n", user);
7349 else debug(D_OPTIONS, "Successfully became user %s.", user);
7352 // --------------------------------------------------------------------
7354 update_every = config_get_number("global", "update every", UPDATE_EVERY);
7355 if(update_every < 1 || update_every > 600) {
7356 fprintf(stderr, "Invalid update timer %d given. Defaulting to %d.\n", update_every, UPDATE_EVERY_MAX);
7357 update_every = UPDATE_EVERY;
7359 else debug(D_OPTIONS, "update timer set to %d.", update_every);
7361 // --------------------------------------------------------------------
7363 listen_port = config_get_number("global", "port", LISTEN_PORT);
7364 if(listen_port < 1 || listen_port > 65535) {
7365 fprintf(stderr, "Invalid listen port %d given. Defaulting to %d.\n", listen_port, LISTEN_PORT);
7366 listen_port = LISTEN_PORT;
7368 else debug(D_OPTIONS, "listen port set to %d.", listen_port);
7370 listen_fd = create_listen_socket(listen_port);
7373 // never become a problem
7374 if(nice(20) == -1) {
7375 fprintf(stderr, "Cannot lower my CPU priority. Error: %s.\n", strerror(errno));
7378 if(become_daemon(0, input_log_file, output_log_file, error_log_file, access_log_file, &access_fd, &stdaccess) == -1) {
7379 fprintf(stderr, "Cannot demonize myself (%s).", strerror(errno));
7384 if(output_log_syslog || error_log_syslog || access_log_syslog)
7385 openlog("netdata", LOG_PID, LOG_DAEMON);
7387 info("NetData started on pid %d", getpid());
7390 // catch all signals
7391 for (i = 1 ; i < 65 ;i++) if(i != SIGSEGV && i != SIGFPE) signal(i, sig_handler);
7393 // spawn childs to collect data
7394 if(config_get_boolean("plugins", "tc", 1)) {
7395 p_tc = malloc(sizeof(pthread_t));
7396 if(pthread_create(p_tc, NULL, tc_main, NULL))
7397 error("failed to create new thread for tc.");
7398 else if(pthread_detach(*p_tc))
7399 error("Cannot request detach of newly created tc thread.");
7402 if(config_get_boolean("plugins", "idlejitter", 1)) {
7403 p_jitter = malloc(sizeof(pthread_t));
7404 if(pthread_create(p_jitter, NULL, cpuidlejitter_main, NULL))
7405 error("failed to create new thread for idlejitter.");
7406 else if(pthread_detach(*p_jitter))
7407 error("Cannot request detach of newly created idlejitter thread.");
7410 if(config_get_boolean("plugins", "proc", 1)) {
7411 p_proc = malloc(sizeof(pthread_t));
7412 if(pthread_create(p_proc, NULL, proc_main, NULL))
7413 error("failed to create new thread for proc.");
7414 else if(pthread_detach(*p_proc))
7415 error("Cannot request detach of newly created proc thread.");
7418 p_pluginsd = malloc(sizeof(pthread_t));
7419 if(pthread_create(p_pluginsd, NULL, pluginsd_main, NULL))
7420 error("failed to create new thread for plugins.d.");
7421 else if(pthread_detach(*p_pluginsd))
7422 error("Cannot request detach of newly created plugins.d thread.");
7424 if(config_get_boolean("plugins", "checks", 1)) {
7425 p_checks = malloc(sizeof(pthread_t));
7426 if(pthread_create(p_checks, NULL, checks_main, NULL))
7427 error("failed to create new thread for checks.");
7428 else if(pthread_detach(*p_checks))
7429 error("Cannot request detach of newly created checks thread.");
7432 // the main process - the web server listener
7434 socket_listen_main(NULL);