File: rrdtool.c

package info (click to toggle)
uwsgi 2.0.31-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,624 kB
  • sloc: ansic: 87,072; python: 7,010; cpp: 1,133; java: 708; perl: 678; sh: 585; ruby: 555; makefile: 148; xml: 130; cs: 121; objc: 37; php: 28; erlang: 20; javascript: 11
file content (191 lines) | stat: -rw-r--r-- 4,827 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <uwsgi.h>

extern struct uwsgi_server uwsgi;

static struct uwsgi_rrdtool {
	void *lib;
	char *lib_name;

	int (*create)(int, char **);
	int (*update)(int, char **);

	int freq;

	char *update_area;
	struct uwsgi_string_list *directory;

	struct uwsgi_stats_pusher *pusher;
} u_rrd;

static struct uwsgi_option rrdtool_options[] = {
	{"rrdtool", required_argument, 0, "store rrd files in the specified directory", uwsgi_opt_add_string_list, &u_rrd.directory, UWSGI_OPT_MASTER|UWSGI_OPT_METRICS},
	{"rrdtool-freq", required_argument, 0, "set collect frequency", uwsgi_opt_set_int, &u_rrd.freq, 0},
	{"rrdtool-lib", required_argument, 0, "set the name of rrd library (default: librrd.so)", uwsgi_opt_set_str, &u_rrd.lib_name, 0},
	{0, 0, 0, 0, 0, 0, 0},

};


static int rrdtool_init() {

	if (!u_rrd.lib_name) {
		u_rrd.lib_name = "librrd.so";
	}

	u_rrd.lib = dlopen(u_rrd.lib_name, RTLD_LAZY);
	if (!u_rrd.lib) return -1;

	u_rrd.create = dlsym(u_rrd.lib, "rrd_create");
	if (!u_rrd.create) {
		dlclose(u_rrd.lib);
		return -1;
	}

	u_rrd.update = dlsym(u_rrd.lib, "rrd_update");
	if (!u_rrd.update) {
		dlclose(u_rrd.lib);
		return -1;
	}

	uwsgi_log_initial("*** RRDtool library available at %p ***\n", u_rrd.lib);

	return 0;
}

/*

	create .rrd files if needed

*/
static void rrdtool_post_init() {

	if (!u_rrd.create) return;

	// do not waste time if no --rrdtool option is defined
	if (!u_rrd.directory) return;

	if (!u_rrd.freq) u_rrd.freq = 300;

	char *argv[7];
	argv[0] = "create";


	// create RRA
	argv[3] = "RRA:AVERAGE:0.5:1:288" ;
	argv[4] = "RRA:AVERAGE:0.5:12:168" ;
	argv[5] = "RRA:AVERAGE:0.5:288:31" ;
	argv[6] = "RRA:AVERAGE:0.5:2016:52";

	struct uwsgi_string_list *usl = NULL;
	uwsgi_foreach(usl, u_rrd.directory) {
		char *dir = uwsgi_expand_path(usl->value, strlen(usl->value), NULL);
                if (!dir) {
                        uwsgi_error("rrdtool_post_init()/uwsgi_expand_path()");
                        exit(1);
                }
		struct uwsgi_metric *um = uwsgi.metrics;
		// here locking is useless, but maybe in the future we could move this part
		// somewhere else
		int created = 0;
		uwsgi_rlock(uwsgi.metrics_lock);
		while(um) {
			char *filename = uwsgi_concat4(dir, "/", um->name, ".rrd");
			if (!uwsgi_file_exists(filename)) {
				argv[1] = filename;
				if (um->type == UWSGI_METRIC_GAUGE) {
					argv[2] = "DS:metric:GAUGE:600:0:U";
				}
				else {
					argv[2] = "DS:metric:DERIVE:600:0:U";
				}
				if (u_rrd.create(7, argv)) {
					uwsgi_log("unable to create rrd file for metric \"%s\"\n", um->name);
					uwsgi_error("rrd_create()");
					exit(1);
				}
				created++;
			}
			free(filename);
			um = um->next;
		}
		uwsgi_rwunlock(uwsgi.metrics_lock);
	
		uwsgi_log("created %d new rrd files in %s\n", created, dir);

		struct uwsgi_stats_pusher_instance *uspi = uwsgi_stats_pusher_add(u_rrd.pusher, NULL);
        	uspi->freq = u_rrd.freq;
		uspi->data = dir;
        	// no need to generate the json
        	uspi->raw=1;
	}

}

static void rrdtool_push(struct uwsgi_stats_pusher_instance *uspi, time_t now, char *json, size_t json_len) {

	if (!u_rrd.update) return ;

	// standard stats pusher
	if (!uspi->data) {
		if (!uspi->arg) {
			uwsgi_log("invalid rrdtool stats pusher syntax\n");
			exit(1);
		}
		uspi->data = uwsgi_expand_path(uspi->arg, strlen(uspi->arg), NULL);
		if (!uspi->data) {
			uwsgi_error("rrdtool_push()/uwsgi_expand_path()");
                        exit(1);
		}
		if (!u_rrd.freq) u_rrd.freq = 300;
		uspi->freq = u_rrd.freq;
	}

	// 1k will be more than enough
	char buf[1024];
	char *argv[3];
	argv[0] = "update";

	struct uwsgi_metric *um = uwsgi.metrics;
	while(um) {
		uwsgi_rlock(uwsgi.metrics_lock);
		int ret = snprintf(buf, 1024, "N:%lld", (long long) (*um->value));
		uwsgi_rwunlock(uwsgi.metrics_lock);
		if (um->reset_after_push){
			uwsgi_wlock(uwsgi.metrics_lock);
			*um->value = um->initial_value;
			uwsgi_rwunlock(uwsgi.metrics_lock);
		}
		if (ret < 3 || ret >= 1024) {
			uwsgi_log("unable to update rrdtool metric for %s\n", um->name);
			goto next;
		}		
		char *filename = uwsgi_concat4(uspi->data, "/", um->name, ".rrd");
		argv[1] = filename;
                argv[2] = buf;
                if (u_rrd.update(3, argv)) {
                	uwsgi_log_verbose("ERROR: rrd_update(\"%s\", \"%s\")\n", argv[1], argv[2]);
                }
		free(filename);
next:
		um = um->next;
	}

}

static void rrdtool_register() {
	u_rrd.pusher = uwsgi_register_stats_pusher("rrdtool", rrdtool_push);
	u_rrd.pusher->raw = 1;
}

struct uwsgi_plugin rrdtool_plugin = {

	.name = "rrdtool",
	.options = rrdtool_options,
	
	.on_load = rrdtool_register,

	// this is the best phase to create rrd files (if needed)
	.preinit_apps = rrdtool_post_init,
	// here we get pointers to rrdtool functions
	.init = rrdtool_init,
};