Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / other / irc2 @ d112f623

Historique | Voir | Annoter | Télécharger (7,98 ko)

1 06e10a53 Robin H. Johnson
#!/usr/bin/perl
2
# -*- perl -*-
3
4
=head1 NAME
5
6
ircstats  - Plugin to graph data about an IRC network and a single IRC server
7
8
=head1 CONFIGURATION
9
10
- ENV{SERVER} to point to the server to connect to, defaults to localhost.
11
- ENV{NICK} nickname to use, defaults to munin-$HASH.
12
13
=head1 USAGE
14
15
This plugin connects to an IRC server.
16
17
=head1 AUTHOR
18
19
Robin H. Johnson
20
21
=head1 LICENSE
22
23
3-clause BSD.
24
25
=head1 MAGIC MARKERS
26
27
  #%# family=manual
28
29
=cut
30
use strict;
31
use warnings;
32
use POE qw(Component::IRC);
33
use Digest::MD5 qw(md5_hex);
34
35
my $nickname = $ENV{NICK} || 'munin-'.md5_hex(rand().time());
36
my $ircname = "Munin statistics gathering from $ENV{FQDN}";
37
my $server = $ENV{SERVER} || 'localhost';
38
39
if($ARGV[0] and $ARGV[0] eq "config") {
40
    print "host_name $server\n";
41
    print "graph_title ircd status - $server\n";
42
    print "graph_order clients channels servers localclients clientmax localclientmax localservers opers unknownconns\n";
43
    print "graph_args -l 0\n";
44
    print "clients.label clients\n";
45
    print "clients.draw LINE2\n";
46
    print "channels.label channels\n";
47
    print "channels.draw LINE2\n";
48
    print "servers.label servers\n";
49
    print "servers.draw LINE2\n";
50
    print "localclients.label localclients\n";
51
    print "localclients.draw LINE2\n";
52
    print "clientmax.label clientmax\n";
53
    print "clientmax.draw LINE2\n";
54
    print "localclientmax.label localclientmax\n";
55
    print "localclientmax.draw LINE2\n";
56
    print "opers.label opers\n";
57
    print "opers.draw LINE2\n";
58
    print "localservers.label localservers\n";
59
    print "localservers.draw LINE2\n";
60
    print "unknownconns.label unknownconns\n";
61
    print "unknownconns.draw LINE2\n";
62
    exit 0;
63
}
64
65
my %result;
66
67
# We create a new PoCo-IRC object
68
my $irc = POE::Component::IRC->spawn( 
69
   nick => $nickname,
70
   ircname => $ircname,
71
   server => $server,
72
   raw => 0,
73
   useipv6 => 0,
74
) or die "Oh noooo! $!";
75
76
POE::Session->create(
77
    package_states => [
78
        main => [ qw(_start irc_001 irc_251 irc_252 irc_253 irc_254 irc_255 irc_265 irc_266 irc_372 irc_375 irc_376 irc_public irc_disconnected ) ], # _default
79
    ],
80
    heap => { irc => $irc },
81
);
82
83
$poe_kernel->run();
84
85
my $RPL_LUSER_CLIENT = 251;
86
my $RPL_LUSERCHANNELS = 254;
87
my $RPL_ENDOFMOTD = 376;
88
89
sub _start {
90
	my ($heap,$kernel,$sender) = @_[HEAP,KERNEL,SENDER];
91
92
    # retrieve our component's object from the heap where we stashed it
93
    my $irc = $heap->{irc};
94
95
    #$irc->yield( register => {("001", "$RPL_LUSER_CLIENT", "$RPL_LUSERCHANNELS", "$RPL_ENDOFMOTD", 'disconnected', 'public', 'all')} );
96
    $irc->yield( register => qw(001 251 252 253 254 255 265 266 372 375 376 disconnected public all) );
97
	#$kernel->post( $sender => register => qw(001 251 254 376 disconnected public all));
98
	#$kernel->post($sender, 'register', qw(001 251 254 376 disconnected public all));
99
    $irc->yield( connect => { } );
100
    return;
101
}
102
103
sub irc_001 {
104
    my $sender = $_[SENDER];
105
106
    # Since this is an irc_* event, we can get the component's object by
107
    # accessing the heap of the sender. Then we register and connect to the
108
    # specified server.
109
    my $irc = $sender->get_heap();
110
111
    #print "Connected to ", $irc->server_name(), "\n";
112
113
    # we join our channels
114
    #$irc->yield( join => $_ ) for @channels;
115
	#sleep 1;
116
	$irc->yield( quit => { });
117
    return;
118
}
119
120
121
#irc_251:  'moo.us.p2p-network.net' 'There are 155 users and 3397 invisible on 16 servers' [There are 155 users and 3397 invisible on 16 servers] 
122
# luserclient
123
sub irc_251 {
124
    #print "In 251\n";
125
    my $sender = $_[SENDER];
126
    my $irc = $sender->get_heap();
127
	my $s = $_[ARG1];
128
	# Do we have something like an UnrealIRCD?
129
	if($s =~  /There are (\d+) users and (\d+) invisible on (\d+) servers/) {
130
		$result{'clients'} = $1 + $2 - 1; # don't count this script
131
		$result{'servers'} = $3;
132
	}
133
    # Or maybe some freendode hyperion stuff?
134
	elsif($s =~  /There are (\d+) listed and (\d+) unlisted users on (\d+) servers/) {
135
		$result{'clients'} = $1 + $2 - 1; # don't count this script
136
		$result{'servers'} = $3;
137
	}
138
    # Or some recent ircnet ircd?
139
    elsif($s =~  /There are (\d+) users and \d+ services on (\d+) servers/) {
140
		$result{'clients'} = $1 - 1; # don't count this script
141
		$result{'servers'} = $2;
142
    }
143
    # Anything else goes here
144
    elsif($s =~  /There are (\d+) users and (\d+) invisible/) {
145
		$result{'clients'} = $1 + $2 - 1; # don't count this script
146
    }
147
    # And here (if there are no invisible count)
148
    elsif($s =~  /There are (\d+) users/) {
149
		$result{'clients'} = $1 - 1; # don't count this script
150
    }
151
	#printf "251 Got clients=%d servers=%d\n", ($result{'clients'} || -1), ($result{'servers'} || -1);
152
}
153
154
#irc_252:  'moo.us.p2p-network.net' '18 :operator(s) online' [18, operator(s) online] 
155
# opers
156
sub irc_252 {
157
    my $sender = $_[SENDER];
158
    my $irc = $sender->get_heap();
159
	my $s = $_[ARG1];
160
    #print "In 252: $s\n";
161
    if($s =~  /^(\d+)/) {
162
		$result{'opers'} = $1;
163
    }
164
	#printf "254 Got channels %d\n", ($result{'channels'} || -1);
165
}
166
167
#irc_253:  'moo.us.p2p-network.net' '1 :unknown connection(s)' [1, unknown connection(s)] 
168
sub irc_253 {
169
    my $sender = $_[SENDER];
170
    my $irc = $sender->get_heap();
171
	my $s = $_[ARG1];
172
    #print "In 253: $s\n";
173
    if($s =~  /^(\d+)/) {
174
		$result{'unknownconns'} = $1;
175
    }
176
	#printf "254 Got channels %d\n", ($result{'channels'} || -1);
177
}
178
179
#irc_254:  'moo.us.p2p-network.net' '1325 :channels formed' [1325, channels formed] 
180
# luserchannels
181
sub irc_254 {
182
    my $sender = $_[SENDER];
183
    my $irc = $sender->get_heap();
184
	my $s = $_[ARG1];
185
    #print "In 254: $s\n";
186
    if($s =~  /^(\d+)/) {
187
		$result{'channels'} = $1;
188
    }
189
	#printf "254 Got channels %d\n", ($result{'channels'} || -1);
190
}
191
192
#irc_255:  'moo.us.p2p-network.net' 'I have 348 clients and 1 servers' [I have 348 clients and 1 servers] 
193
# local clients/servers
194
sub irc_255 {
195
    my $sender = $_[SENDER];
196
    my $irc = $sender->get_heap();
197
	my $s = $_[ARG1];
198
    #print "In 255: $s\n";
199
	if($s =~  /I have (\d+) clients and (\d+) servers/) {
200
		$result{'localclients'} = $1-1; # don't count this script
201
		$result{'localservers'} = $2;
202
	}
203
}
204
205
#irc_265:  'moo.us.p2p-network.net' 'Current Local Users: 348  Max: 1900' [Current Local Users: 348  Max: 1900] 
206
sub irc_265 {
207
    #print "In 265\n";
208
    my $sender = $_[SENDER];
209
    my $irc = $sender->get_heap();
210
	my $s = $_[ARG1];
211
    #print "In 265: $s\n";
212
	if($s =~  /Current Local Users: (\d+)\s+Max: (\d+)/) {
213
		$result{'localclients'} = $1-1; # don't count this script
214
		$result{'localclientmax'} = $2;
215
	}
216
}
217
218
#irc_266:  'moo.us.p2p-network.net' 'Current Global Users: 3552  Max: 8742' [Current Global Users: 3552  Max: 8742] 
219
sub irc_266 {
220
    #print "In 266\n";
221
	my $sender = $_[SENDER];
222
    my $irc = $sender->get_heap();
223
	my $s = $_[ARG1];
224
    #print "In 266: $s\n";
225
	if($s =~  /Current Global Users: (\d+)\s+Max: (\d+)/) {
226
		$result{'clients'} = $1-1; # don't count this script
227
		$result{'clientmax'} = $2;
228
	}
229
}
230
231
# 372 motdline
232
sub irc_372 {
233
	return;
234
}
235
# 375 startofmotd
236
sub irc_375 {
237
	return;
238
}
239
# 376 endofmotd
240
sub irc_376 {
241
    my $sender = $_[SENDER];
242
    my $irc = $sender->get_heap();
243
	$irc->yield( quit => {} );
244
}
245
246
sub munin_print {
247
	my $key = shift;
248
	my $val = shift;
249
	print "${key}.value ".($val || 'U')."\n";
250
}
251
252
sub irc_disconnected {
253
	for my $var (qw(clients channels servers localclients clientmax localclientmax localservers opers unknownconns)) {
254
		munin_print($var, $result{$var});
255
	}
256
	exit 0;
257
}
258
259
sub irc_public {
260
    my ($sender, $who, $where, $what) = @_[SENDER, ARG0 .. ARG2];
261
    my $nick = ( split /!/, $who )[0];
262
    my $channel = $where->[0];
263
264
    if ( my ($rot13) = $what =~ /^rot13 (.+)/ ) {
265
        $rot13 =~ tr[a-zA-Z][n-za-mN-ZA-M];
266
        $irc->yield( privmsg => $channel => "$nick: $rot13" );
267
    }
268
    return;
269
}
270
271
# We registered for all events, this will produce some debug info.
272
sub _default {
273
    my ($event, $args) = @_[ARG0 .. $#_];
274
    my @output = ( "$event: " );
275
276
    for my $arg (@$args) {
277
        if ( ref $arg eq 'ARRAY' ) {
278
            push( @output, '[' . join(', ', @$arg ) . ']' );
279
        }
280
        else {
281
            push ( @output, "'$arg'" );
282
        }
283
    }
284
    print join ' ', @output, "\n";
285
    return 0;
286
}