comparison misc/munin/prosody_.lua @ 1429:3f85aaca8282

misc/munin/prosody_.lua: Munin script for collecting statistics from mod_statistics
author Kim Alvefur <zash@zash.se>
date Tue, 03 Jun 2014 20:00:50 +0200
parents
children 513332854531
comparison
equal deleted inserted replaced
1428:091ee76745e8 1429:3f85aaca8282
1 #!/usr/bin/env lua
2
3 local print = print;
4 local pairs = pairs;
5 local socket = require"socket";
6
7 local stats = {};
8
9 stats.c2s = {
10 graph_title = "Prosody C2S Connections";
11 graph_vlabel = "users";
12 graph_category = "Prosody";
13 all_client_connections = {
14 label = "client connections";
15 _key = "total_c2s";
16 }
17 }
18
19 stats.s2s = {
20 graph_title = "Prosody S2S Connections";
21 graph_vlabel = "servers";
22 graph_category = "Prosody";
23 outgoing_connections = {
24 label = "outgoing connections";
25 _key = "total_s2sout";
26 };
27 incoming_connections = {
28 label = "incoming connections";
29 _key = "total_s2sin";
30 }
31 }
32
33 stats.mem = {
34 graph_title = "Prosody Memory Usage";
35 graph_vlabel = "Bytes";
36 graph_args = "--base 1024 -l 0";
37 graph_category = "Prosody"; --memory_unused
38 graph_order = "memory_total memory_rss memory_allocated memory_used memory_lua memory_returnable";
39
40 memory_allocated = { label = "Allocated", draw = "AREA" };
41 memory_lua = { label = "Lua", draw = "AREA" };
42 memory_rss = { label = "RSS", draw = "AREA" };
43 memory_total = { label = "Total", draw = "AREA" };
44 -- memory_unused = { label = "Unused", draw = "AREA" };
45 memory_used = { label = "Used", draw = "AREA" };
46 memory_returnable = { label = "Returnable", draw = "AREA" };
47 }
48
49 stats.cpu = {
50 graph_title = "Prosody CPU Usage";
51 graph_category = "Prosody";
52 graph_args = "-l 0";
53 graph_vlabel = "CPU time used in milliseconds";
54
55 cpu_total = { label = "CPU"; type = "DERIVE"; min = 0; };
56 }
57
58 stats.auth = {
59 graph_title = "Prosody Authentications";
60 graph_category = "Prosody";
61 graph_args = "--base 1000";
62
63 c2s_auth = {
64 label = "Logins";
65 type = "DERIVE";
66 min = 0;
67 };
68 c2s_authfail = {
69 label = "Failed logins";
70 type = "DERIVE";
71 min = 0;
72 };
73 }
74
75 local function onerror(msg, err, exit)
76 io.stderr:write(msg, '\n');
77 if err then
78 io.stderr:write(err, '\n');
79 end
80 os.exit(exit or 1);
81 end
82
83
84 local function connect()
85 local conn, err = socket.connect(os.getenv"host" or "localhost", os.getenv"port" or 5782);
86 if not conn then onerror("Could not connect to prosody", err); end
87 conn:settimeout(1);
88 return conn;
89 end
90
91 local function get_config(item)
92 for k,v in pairs(item) do
93 if type(v) == "string" then
94 print(k .. " " .. v);
95 elseif type(v) == "table" then
96 for sk,v in pairs(v) do
97 if not sk:match("^_") then
98 print(k.."."..sk.." "..v);
99 end
100 end
101 end
102 end
103 end
104
105 local function get_stats(item)
106 local labels = {};
107 for key, val in pairs(item) do
108 if type(val) == "table" and val.label then
109 labels[val._key or key] = key;
110 end
111 end
112
113 local conn = connect();
114 local line, err = conn:receive("*l");
115 local stat, value, label;
116 while line and line ~= "" and next(labels) ~= nil do
117 stat, value = line:match('^STAT%s+"([^"]*)"%s*(%b())');
118 label = stat and labels[stat];
119 if label then
120 print(label..".value "..tonumber(value:sub(2,-2)));
121 labels[stat] = nil;
122 end
123 line, err = conn:receive("*l");
124 end
125 if err then onerror(err); end
126 end
127
128 local function main(stat, mode)
129 if mode == "suggest" then
130 for available_stat in pairs(stats) do
131 print(available_stat);
132 end
133 elseif mode == "config" then
134 return get_config(stats[stat]);
135 elseif stats[stat] then
136 return get_stats(stats[stat]);
137 end
138 end
139
140 if arg then return main(arg[0]:match("prosody_(%w*)"), ...); end
141
142 return {
143 stats = stats,
144 get_stats = get_stats,
145 get_config = get_config,
146 }
147