mirror of
https://github.com/webmin/webmin.git
synced 2026-03-20 16:50:24 +00:00
Add support for collecting bandwidth stats using Journald
This commit is contained in:
@@ -41,17 +41,35 @@ my @hours = ( );
|
||||
# Pre-process command
|
||||
&pre_process();
|
||||
|
||||
# Open the log file or pipe to journalctl
|
||||
if ($syslog_journald) {
|
||||
$timestamp_file = "$module_var_directory/last-processed";
|
||||
my $last_processed = 0;
|
||||
if (-r $timestamp_file) {
|
||||
$last_processed = &read_file_contents($timestamp_file);
|
||||
chomp($last_processed);
|
||||
$last_processed = int($last_processed) || 0;
|
||||
}
|
||||
my $journal_cmd = &has_command("journalctl");
|
||||
$journal_cmd = "$journal_cmd -k --since=\@$last_processed ".
|
||||
"--until=\@$time_now --grep=\"BANDWIDTH_(IN|OUT):\"";
|
||||
open($logfh, '-|', $journal_cmd) ||
|
||||
die("Cannot open $journal_cmd pipe: $!\n");
|
||||
}
|
||||
else {
|
||||
open($logfh, "<".$bandwidth_log) ||
|
||||
die("Cannot open $bandwidth_log: $!\n");
|
||||
}
|
||||
|
||||
# Scan the entries in the log file
|
||||
&pre_process();
|
||||
open(LOG, "<".$bandwidth_log);
|
||||
while(<LOG>) {
|
||||
while(<$logfh>) {
|
||||
if (&process_line($_, \@hours, $time_now)) {
|
||||
# Found a valid line
|
||||
$lastline = $_;
|
||||
}
|
||||
elsif (/last\s+message\s+repeated\s+(\d+)/) {
|
||||
# re-process the last line N-1 times
|
||||
for($i=0; $i<$1-1; $i++) {
|
||||
for(my $i=0; $i<$1-1; $i++) {
|
||||
&process_line($lastline, \@hours, $time_now);
|
||||
}
|
||||
}
|
||||
@@ -59,7 +77,7 @@ while(<LOG>) {
|
||||
#print "skipping $_";
|
||||
}
|
||||
}
|
||||
close(LOG);
|
||||
close($logfh);
|
||||
|
||||
# Save all hours
|
||||
foreach my $hour (@hours) {
|
||||
@@ -71,7 +89,14 @@ if (-r $bandwidth_log) {
|
||||
open(my $log, ">".$bandwidth_log);
|
||||
close($log);
|
||||
}
|
||||
&foreign_call($syslog_module, "signal_syslog");
|
||||
&foreign_call($syslog_module, "signal_syslog") if (!$syslog_journald);
|
||||
|
||||
# Save last collection time to start from here next time
|
||||
if ($syslog_journald && @hours) {
|
||||
&lock_file($timestamp_file);
|
||||
&write_file_contents($timestamp_file, $time_now);
|
||||
&unlock_file($timestamp_file);
|
||||
}
|
||||
|
||||
# Remove PID file
|
||||
unlink($pid_file);
|
||||
|
||||
Reference in New Issue
Block a user