-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathstats-lib-funcs.pl
240 lines (228 loc) · 7.43 KB
/
stats-lib-funcs.pl
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
#
# Authentic Theme (https://github.com/authentic-theme/authentic-theme)
# Copyright Ilia Rostovtsev <[email protected]>
# Licensed under MIT (https://github.com/authentic-theme/authentic-theme/blob/master/LICENSE)
#
use strict;
# Import global variables
our ($config_directory, $var_directory, $current_theme);
# Load theme language and settings
our %text = load_language($current_theme);
my %settings = settings("$config_directory/$current_theme/settings.js", 'settings_');
# Check if feature is enabled
sub get_stats_option
{
if ($_[1]) {
return $settings{"settings_sysinfo_real_time_$_[0]"};
}
if (!scalar %settings) {
return 1;
}
return ($settings{"settings_sysinfo_real_time_$_[0]"} eq 'false') ? 0 : 1;
}
# JSON conversion
sub jsonify
{
my $json = shift;
my $json_obj;
eval {
$json_obj = convert_from_json($json);
};
return (ref($json_obj) eq 'HASH' && keys %{$json_obj}) ? $json_obj : {};
}
sub get_stats_empty
{
my $time = time();
return {
graphs =>
{ cpu => [{x => $time, y => 0}],
mem => [{x => $time, y => 0}],
virt => [{x => $time, y => 0}],
proc => [{x => $time, y => 0}],
disk => [{x => $time, y => [0, 0]}],
net => [{x => $time, y => [0, 0]}]
}
};
}
sub get_stats_now
{
my %data;
my $graphs = {};
my $gadd = sub {
my $time = time();
my ($k, $d) = @_;
$graphs->{$k} = [] if (ref($graphs->{$k}) ne 'ARRAY');
push(@{$graphs->{$k}}, {x => $time, y => $d});
};
# Collect stats
if (foreign_check("proc")) {
foreign_require("proc");
# CPU stats
if (acl_system_status('cpu')) {
my @cpuinfo = defined(&proc::get_cpu_info) ? proc::get_cpu_info() : ();
my @cpuusage = defined(&proc::get_cpu_io_usage) ? proc::get_cpu_io_usage() : ();
if (@cpuinfo && @cpuusage) {
# CPU load
my $cpu = int($cpuusage[0] + $cpuusage[1] + $cpuusage[3]);
$data{'cpu'} = [$cpu, text('body_load', ($cpuinfo[0], $cpuinfo[1], $cpuinfo[2]))];
$gadd->('cpu', $cpu);
# Disk I/O
my $in = $cpuusage[5];
my $out = $cpuusage[6];
if ($in && $out || $in eq '0' || $out eq '0') {
$data{'io'} = [$in, $out];
$gadd->('disk', [$in, $out]);
}
}
}
# Memory stats
if (acl_system_status('mem')) {
my @memory = defined(&proc::get_memory_info) ? proc::get_memory_info() : ();
if (@memory) {
$data{'mem'} = [];
$data{'virt'} = [];
if (@memory && $memory[0] && $memory[0] > 0) {
my $mem = (100 - int(($memory[1] / $memory[0]) * 100));
$data{'mem'} = [$mem,
text(($memory[4] ? 'body_used_cached_total' : 'body_used'),
nice_size($memory[0] * 1024),
nice_size(($memory[0] - $memory[1]) * 1024),
($memory[4] ? nice_size($memory[4] * 1024) : undef)
)];
$gadd->('mem', $mem);
}
if (@memory && $memory[2] && $memory[2] > 0) {
my $virt = (100 - int(($memory[3] / $memory[2]) * 100));
$data{'virt'} = [$virt,
text('body_used',
nice_size(($memory[2]) * 1024),
nice_size(($memory[2] - $memory[3]) * 1024)
)];
$gadd->('virt', $virt);
}
}
}
# Number of running processes
if (acl_system_status('load')) {
my @processes = proc::list_processes();
my $proc = scalar(@processes);
$data{'proc'} = $proc;
$gadd->('proc', $proc);
}
}
# Network I/O
if (acl_system_status('load')) {
my $network = network_stats('io');
my $nrs = unserialise_variable($network);
my $in = @{$nrs}[0];
my $out = @{$nrs}[1];
if ($in && $out || $in eq '0' || $out eq '0') {
$data{'net'} = [$in, $out];
$gadd->('net', [$in, $out]);
}
}
# Reverse output for LTR users
if (get_text_ltr()) {
my @watched = ('mem', 'virt', 'disk');
foreach my $key (@watched) {
if ($data{$key} && $data{$key}[1]) {
$data{$key}[1] = reverse_string($data{$key}[1], "/");
}
}
}
$data{'graphs'} = $graphs;
# Return data
return \%data;
}
sub get_stats_history
{
my ($noempty) = @_;
my $file = "$var_directory/modules/$current_theme".
"/real-time-monitoring.json";
my $graphs = jsonify(read_file_contents($file));
# No data yet
if (!keys %{$graphs}) {
unlink($file);
return $noempty ? undef : get_stats_empty();
}
# Check if data is right
foreach my $k (keys %{$graphs}) {
if (ref($graphs->{$k}) ne 'ARRAY') {
$graphs->{$k} = [];
}
}
# Check if still avilable based on current ACLs
my %map = (cpu => ['cpu', 'disk'],
mem => ['mem', 'virt'],
load => ['proc', 'net']);
foreach my $key (keys %map) {
my $feature = acl_system_status($key);
unless ($feature) {
foreach my $skey (@{$map{$key}}) {
delete $graphs->{$skey};
}
}
}
# Trim dataset
trim_stats_history($graphs);
# No data is available anymore
if (!keys %{$graphs}) {
unlink($file);
return $noempty ? undef : get_stats_empty();
}
# Return data
return { graphs => $graphs };
}
sub trim_stats_history
{
my ($graphs) = @_;
my $time = time();
my $get_default_graph = sub {
my ($key, $time) = @_;
my $default = get_stats_empty();
return $default->{'graphs'}{$key};
};
my $n = get_stats_option('stored_duration', 1) || 600;
if ($n < 300 || $n > 3600) {
$n = 600;
}
foreach my $k (keys %{$graphs}) {
my @new_array;
foreach my $entry (@{ $graphs->{$k} }) {
if (defined($entry) && ($time - $entry->{'x'}) <= $n) {
push(@new_array, $entry);
}
}
$graphs->{$k} =
@new_array ? \@new_array : $get_default_graph->($k, $time);
}
}
sub merge_stats {
my ($graphs1, $graphs2) = @_;
foreach my $key (keys %{$graphs2}) {
if (exists($graphs1->{$key})) {
push(@{$graphs1->{$key}}, @{$graphs2->{$key}});
} else {
$graphs1->{$key} = $graphs2->{$key};
}
}
return $graphs1;
}
sub save_stats_history
{
# Store complete dataset
my ($graphs_chunk) = @_;
# Load stored data
my $all_stats_histoy = get_stats_history()->{'graphs'};
# Merge data
my $graphs = merge_stats($all_stats_histoy, $graphs_chunk);
# Trim dataset
trim_stats_history($graphs);
# Save data
my $file = "$var_directory/modules/$current_theme".
"/real-time-monitoring.json";
lock_file($file);
write_file_contents($file, convert_to_json($graphs));
unlock_file($file);
}
1;