-
Notifications
You must be signed in to change notification settings - Fork 235
/
uptime.pl
138 lines (105 loc) · 2.93 KB
/
uptime.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
#
# Copyright (C) 2002-2021 by Peder Stray <[email protected]>
#
use strict;
use Irssi;
use Irssi::Irc;
use Irssi::TextUI;
use vars qw{$VERSION %IRSSI};
($VERSION) = '$Revision: 1.6.1 $' =~ / (\d+(\.\d+)+) /;
%IRSSI = (
name => 'uptime',
authors => 'Peder Stray',
url => 'https://github.com/pstray/irssi-uptime',
license => 'GPL',
description => 'Try a little harder to figure out client uptime',
sbitem => 'uptime',
);
my($timer) = 0; # ID of current timer
sub uptime_linux {
my($sys_uptime);
my($irssi_start);
local(*FILE);
open FILE, "<", "/proc/uptime";
$sys_uptime = (split " ", <FILE>)[0];
close FILE;
open FILE, "<", "/proc/$$/stat";
$irssi_start = (split " ", <FILE>)[21];
close FILE;
return $sys_uptime - $irssi_start/100;
}
sub uptime_solaris {
my($irssi_start);
$irssi_start = time - (stat("/proc/$$"))[9];
return $irssi_start;
}
sub uptime {
my($sysname) = @_;
my($time);
if ($sysname eq 'Linux') {
$time = uptime_linux;
} elsif ($sysname eq 'SunOS') {
$time = uptime_solaris;
} else {
$time = time - $^T;
}
return $time;
}
sub format_interval {
my($interval) = @_;
my(@interval,$str);
for (60, 60, 24, 365) {
push @interval, $interval%$_;
$interval = int($interval/$_);
}
$str = sprintf "%dy %dd %dh %dm %ds", $interval, @interval[3,2,1,0];
$str =~ s/^(0. )+//;
return $str;
}
sub cmd_uptime {
my($data,$server,$witem) = @_;
my($sysname) = Irssi::parse_special('$sysname');
my($uptime) = uptime($sysname);
my($str) = format_interval($uptime);
if ($data && $server) {
$server->command("MSG $data uptime: $str");
} elsif ($witem && ($witem->{type} eq "CHANNEL" ||
$witem->{type} eq "QUERY")) {
$witem->command("MSG ".$witem->{name}." uptime: $str");
} else {
Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'uptime',
$str, $sysname);
}
}
sub sig_setup_changed {
my($interval) = Irssi::settings_get_int('uptime_refresh_interval');
Irssi::timeout_remove($timer);
if ($interval < 1) {
$interval = 0;
}
return unless $interval;
$interval *= 1000;
$timer = Irssi::timeout_add($interval, 'uptime_refresh' , undef);
}
sub sb_uptime {
my($item, $get_size_only) = @_;
my $format = "";
my($uptime) = uptime(Irssi::parse_special('$sysname'));
my($time) = format_interval($uptime);
$format = "{sb %g$time%n}";
$item->default_handler($get_size_only, $format, undef, 1);
}
sub uptime_refresh {
Irssi::statusbar_items_redraw('uptime');
}
Irssi::command_bind('uptime', 'cmd_uptime');
Irssi::theme_register(
[
'uptime',
'{line_start}{hilight Uptime:} $0 ($1)',
]);
Irssi::settings_add_int('upgrade', 'uptime_refresh_interval', 12);
Irssi::signal_add('setup changed', 'sig_setup_changed');
Irssi::statusbar_item_register('uptime', undef, 'sb_uptime');
sig_setup_changed;