forked from cdemi/PRTG-Redis-Sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
362 lines (355 loc) · 19 KB
/
Program.cs
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Diagnostics;
using System.Linq;
namespace PRTG_Redis_Sensor
{
internal class Program
{
private static void Main(string[] args)
{
ConfigurationOptions configurationOptions = new ConfigurationOptions() {
EndPoints =
{
{ args[0] }
},
AllowAdmin = true
};
if (args.Length > 1)
{
configurationOptions.Password = args[1];
}
ConnectionMultiplexer redisConnectionMultiplexer = ConnectionMultiplexer.Connect(configurationOptions);
IServer redisServer = redisConnectionMultiplexer.GetServer(configurationOptions.EndPoints[0]);
IGrouping<string, System.Collections.Generic.KeyValuePair<string, string>>[] info = redisServer.Info();
IGrouping<string, System.Collections.Generic.KeyValuePair<string, string>> serverInfo = info.SingleOrDefault(i => i.Key.Equals("Server", StringComparison.InvariantCultureIgnoreCase));
IGrouping<string, System.Collections.Generic.KeyValuePair<string, string>> clientsInfo = info.SingleOrDefault(i => i.Key.Equals("Clients", StringComparison.InvariantCultureIgnoreCase));
IGrouping<string, System.Collections.Generic.KeyValuePair<string, string>> memoryInfo = info.SingleOrDefault(i => i.Key.Equals("Memory", StringComparison.InvariantCultureIgnoreCase));
IGrouping<string, System.Collections.Generic.KeyValuePair<string, string>> persistenceInfo = info.SingleOrDefault(i => i.Key.Equals("Persistence", StringComparison.InvariantCultureIgnoreCase));
IGrouping<string, System.Collections.Generic.KeyValuePair<string, string>> statsInfo = info.SingleOrDefault(i => i.Key.Equals("Stats", StringComparison.InvariantCultureIgnoreCase));
IGrouping<string, System.Collections.Generic.KeyValuePair<string, string>> replicationInfo = info.SingleOrDefault(i => i.Key.Equals("Replication", StringComparison.InvariantCultureIgnoreCase));
IGrouping<string, System.Collections.Generic.KeyValuePair<string, string>> keyspaceInfo = info.SingleOrDefault(i => i.Key.Equals("Keyspace", StringComparison.InvariantCultureIgnoreCase));
IGrouping<string, System.Collections.Generic.KeyValuePair<string, string>> cpuInfo = info.SingleOrDefault(i => i.Key.Equals("CPU", StringComparison.InvariantCultureIgnoreCase));
IGrouping<string, System.Collections.Generic.KeyValuePair<string, string>> clusterInfo = info.SingleOrDefault(i => i.Key.Equals("Cluster", StringComparison.InvariantCultureIgnoreCase));
var response =
new
{
prtg = new PRTGResponse()
{
result = new System.Collections.Generic.List<PRTGResult>
{
{
new PRTGResult()
{
channel = "Uptime",
unit = PRTGUnit.TimeSeconds,
value = serverInfo.SingleOrDefault(i => i.Key.Equals("uptime_in_seconds")).Value
}
},
{
new PRTGResult()
{
channel = "Connected Clients",
unit = PRTGUnit.Count,
value = clientsInfo.SingleOrDefault(i => i.Key.Equals("connected_clients")).Value
}
},
{
new PRTGResult()
{
channel = "Blocked Clients",
unit = PRTGUnit.Count,
value = clientsInfo.SingleOrDefault(i => i.Key.Equals("blocked_clients")).Value
}
},
{
new PRTGResult()
{
channel = "Used Memory",
unit = PRTGUnit.BytesMemory,
value = memoryInfo.SingleOrDefault(i => i.Key.Equals("used_memory")).Value
}
},
{
new PRTGResult()
{
channel = "Used Memory RSS",
unit = PRTGUnit.BytesMemory,
value = memoryInfo.SingleOrDefault(i => i.Key.Equals("used_memory_rss")).Value
}
},
{
new PRTGResult()
{
channel = "Used Memory Peak",
unit = PRTGUnit.BytesMemory,
value = memoryInfo.SingleOrDefault(i => i.Key.Equals("used_memory_peak")).Value
}
},
{
new PRTGResult()
{
channel = "Memory Fragmentation Ratio",
unit = PRTGUnit.Custom,
Float = 1,
value = memoryInfo.SingleOrDefault(i => i.Key.Equals("mem_fragmentation_ratio")).Value
}
},
{
new PRTGResult()
{
channel = "RDB Last Background Save Status",
unit = PRTGUnit.Count,
ShowChart = 0,
value = persistenceInfo.SingleOrDefault(i => i.Key.Equals("rdb_last_bgsave_status")).Value == "ok" ? "1" : "0"
}
},
{
new PRTGResult()
{
channel = "RDB Last Background Save Time in Seconds",
unit = PRTGUnit.TimeSeconds,
value = persistenceInfo.SingleOrDefault(i => i.Key.Equals("rdb_last_bgsave_time_sec")).Value
}
},
{
new PRTGResult()
{
channel = "AOF Last Rewrite Time in Seconds",
unit = PRTGUnit.TimeSeconds,
value = persistenceInfo.SingleOrDefault(i => i.Key.Equals("aof_last_rewrite_time_sec")).Value
}
},
{
new PRTGResult()
{
channel = "AOF Last Background Rewrite Status",
unit = PRTGUnit.Count,
ShowChart = 0,
value = persistenceInfo.SingleOrDefault(i => i.Key.Equals("aof_last_bgrewrite_status")).Value == "ok" ? "1" : "0"
}
},
{
new PRTGResult()
{
channel = "AOF Last Write Status",
unit = PRTGUnit.Count,
ShowChart = 0,
value = persistenceInfo.SingleOrDefault(i => i.Key.Equals("aof_last_write_status")).Value == "ok" ? "1" : "0"
}
},
{
new PRTGResult()
{
channel = "Total Connections Received",
unit = PRTGUnit.Count,
value = statsInfo.SingleOrDefault(i => i.Key.Equals("total_connections_received")).Value
}
},
{
new PRTGResult()
{
channel = "Total Commands Processed",
unit = PRTGUnit.Count,
value = statsInfo.SingleOrDefault(i => i.Key.Equals("total_commands_processed")).Value
}
},
{
new PRTGResult()
{
channel = "Instantaneous Operations per Second",
unit = PRTGUnit.Count,
value = statsInfo.SingleOrDefault(i => i.Key.Equals("instantaneous_ops_per_sec")).Value
}
},
{
new PRTGResult()
{
channel = "Total Net Input Bytes",
unit = PRTGUnit.BytesBandwidth,
value = statsInfo.SingleOrDefault(i => i.Key.Equals("total_net_input_bytes")).Value
}
},
{
new PRTGResult()
{
channel = "Total Net Output Bytes",
unit = PRTGUnit.BytesBandwidth,
value = statsInfo.SingleOrDefault(i => i.Key.Equals("total_net_output_bytes")).Value
}
},
{
new PRTGResult()
{
channel = "Rejected Connections",
unit = PRTGUnit.Count,
value = statsInfo.SingleOrDefault(i => i.Key.Equals("rejected_connections")).Value
}
},
{
new PRTGResult()
{
channel = "Pubsub Channels",
unit = PRTGUnit.Count,
value = statsInfo.SingleOrDefault(i => i.Key.Equals("pubsub_channels")).Value
}
},
{
new PRTGResult()
{
channel = "Pubsib Patterns",
unit = PRTGUnit.Count,
value = statsInfo.SingleOrDefault(i => i.Key.Equals("pubsub_patterns")).Value
}
},
{
new PRTGResult()
{
channel = "Is Master",
unit = PRTGUnit.Count,
ShowChart = 0,
value = replicationInfo.SingleOrDefault(i => i.Key.Equals("role")).Value.Equals("master", StringComparison.InvariantCultureIgnoreCase) ? "1" : "0"
}
},
{
new PRTGResult()
{
channel = "Is Replication Backlog Active",
unit = PRTGUnit.Count,
ShowChart = 0,
value = replicationInfo.SingleOrDefault(i => i.Key.Equals("repl_backlog_active")).Value
}
},
{
new PRTGResult()
{
channel = "Replication Backlog Size",
unit = PRTGUnit.BytesMemory,
value = replicationInfo.SingleOrDefault(i => i.Key.Equals("repl_backlog_size")).Value
}
}
,
{
new PRTGResult()
{
channel = "Replication Backlog First Size Byte Offset",
unit = PRTGUnit.BytesMemory,
value = replicationInfo.SingleOrDefault(i => i.Key.Equals("repl_backlog_first_byte_offset")).Value
}
},
{
new PRTGResult()
{
channel = "Replication Backlog Histlen",
unit = PRTGUnit.BytesMemory,
value = replicationInfo.SingleOrDefault(i => i.Key.Equals("repl_backlog_histlen")).Value
}
},
{
new PRTGResult()
{
channel = "Connected Slaves",
unit = PRTGUnit.Count,
value = replicationInfo.SingleOrDefault(i => i.Key.Equals("connected_slaves")).Value
}
},
{
new PRTGResult()
{
channel = "Keys",
unit = PRTGUnit.Count,
value = SafeGetInt32(() => keyspaceInfo != null ? keyspaceInfo.SingleOrDefault(i => i.Key.Equals("db0")).Value.Split(',').Select(x => x.Split('=')).ToDictionary(x => x[0], x => x[1])["keys"] : "0")
}
},
{
new PRTGResult()
{
channel = "Keys Expires",
unit = PRTGUnit.Count,
value = SafeGetInt32(() => keyspaceInfo != null ? keyspaceInfo.SingleOrDefault(i => i.Key.Equals("db0")).Value.Split(',').Select(x => x.Split('=')).ToDictionary(x => x[0], x => x[1])["expires"] : "0")
}
},
{
new PRTGResult()
{
channel = "keyspace_hits",
unit = PRTGUnit.Count,
value = statsInfo.SingleOrDefault(i => i.Key.Equals("keyspace_hits")).Value
}
},
{
new PRTGResult()
{
channel = "keyspace_misses",
unit = PRTGUnit.Count,
value = statsInfo.SingleOrDefault(i => i.Key.Equals("keyspace_misses")).Value
}
},
{
new PRTGResult()
{
channel = "Used CPU Sys",
unit = PRTGUnit.Custom,
Float=1,
value = cpuInfo.SingleOrDefault(i => i.Key.Equals("used_cpu_sys")).Value
}
},
{
new PRTGResult()
{
channel = "Used CPU User",
unit = PRTGUnit.Custom,
Float=1,
value = cpuInfo.SingleOrDefault(i => i.Key.Equals("used_cpu_user")).Value
}
},
{
new PRTGResult()
{
channel = "Used CPU Sys Children",
unit = PRTGUnit.Custom,
Float=1,
value = cpuInfo.SingleOrDefault(i => i.Key.Equals("used_cpu_sys_children")).Value
}
},
{
new PRTGResult()
{
channel = "Used CPU User Children",
unit = PRTGUnit.Custom,
Float=1,
value = cpuInfo.SingleOrDefault(i => i.Key.Equals("used_cpu_user_children")).Value
}
},
{
new PRTGResult()
{
channel = "Cluster Enabled",
unit = PRTGUnit.Count,
ShowChart = 0,
value = clusterInfo.SingleOrDefault(i => i.Key.Equals("cluster_enabled")).Value
}
}
}
}
};
Console.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
}));
}
private static string SafeGetInt32(Func<string> func)
{
try
{
return func();
}
catch (Exception ex)
{
Trace.TraceError(ex.ToString());
return "0";
}
}
}
}