Projet

Général

Profil

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

root / plugins / ircd / irc2 @ 17f78427

Historique | Voir | Annoter | Télécharger (8 ko)

1
#!/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_category chat\n";
43
    print "graph_order clients channels servers localclients clientmax localclientmax localservers opers unknownconns\n";
44
    print "graph_args -l 0\n";
45
    print "clients.label clients\n";
46
    print "clients.draw LINE2\n";
47
    print "channels.label channels\n";
48
    print "channels.draw LINE2\n";
49
    print "servers.label servers\n";
50
    print "servers.draw LINE2\n";
51
    print "localclients.label localclients\n";
52
    print "localclients.draw LINE2\n";
53
    print "clientmax.label clientmax\n";
54
    print "clientmax.draw LINE2\n";
55
    print "localclientmax.label localclientmax\n";
56
    print "localclientmax.draw LINE2\n";
57
    print "opers.label opers\n";
58
    print "opers.draw LINE2\n";
59
    print "localservers.label localservers\n";
60
    print "localservers.draw LINE2\n";
61
    print "unknownconns.label unknownconns\n";
62
    print "unknownconns.draw LINE2\n";
63
    exit 0;
64
}
65

    
66
my %result;
67

    
68
# We create a new PoCo-IRC object
69
my $irc = POE::Component::IRC->spawn(
70
   nick => $nickname,
71
   ircname => $ircname,
72
   server => $server,
73
   raw => 0,
74
   useipv6 => 0,
75
) or die "Oh noooo! $!";
76

    
77
POE::Session->create(
78
    package_states => [
79
        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
80
    ],
81
    heap => { irc => $irc },
82
);
83

    
84
$poe_kernel->run();
85

    
86
my $RPL_LUSER_CLIENT = 251;
87
my $RPL_LUSERCHANNELS = 254;
88
my $RPL_ENDOFMOTD = 376;
89

    
90
sub _start {
91
	my ($heap,$kernel,$sender) = @_[HEAP,KERNEL,SENDER];
92

    
93
    # retrieve our component's object from the heap where we stashed it
94
    my $irc = $heap->{irc};
95

    
96
    #$irc->yield( register => {("001", "$RPL_LUSER_CLIENT", "$RPL_LUSERCHANNELS", "$RPL_ENDOFMOTD", 'disconnected', 'public', 'all')} );
97
    $irc->yield( register => qw(001 251 252 253 254 255 265 266 372 375 376 disconnected public all) );
98
	#$kernel->post( $sender => register => qw(001 251 254 376 disconnected public all));
99
	#$kernel->post($sender, 'register', qw(001 251 254 376 disconnected public all));
100
    $irc->yield( connect => { } );
101
    return;
102
}
103

    
104
sub irc_001 {
105
    my $sender = $_[SENDER];
106

    
107
    # Since this is an irc_* event, we can get the component's object by
108
    # accessing the heap of the sender. Then we register and connect to the
109
    # specified server.
110
    my $irc = $sender->get_heap();
111

    
112
    #print "Connected to ", $irc->server_name(), "\n";
113

    
114
    # we join our channels
115
    #$irc->yield( join => $_ ) for @channels;
116
	#sleep 1;
117
	$irc->yield( quit => { });
118
    return;
119
}
120

    
121

    
122
#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]
123
# luserclient
124
sub irc_251 {
125
    #print "In 251\n";
126
    my $sender = $_[SENDER];
127
    my $irc = $sender->get_heap();
128
	my $s = $_[ARG1];
129
	# Do we have something like an UnrealIRCD?
130
	if($s =~  /There are (\d+) users and (\d+) invisible on (\d+) servers/) {
131
		$result{'clients'} = $1 + $2 - 1; # don't count this script
132
		$result{'servers'} = $3;
133
	}
134
    # Or maybe some freendode hyperion stuff?
135
	elsif($s =~  /There are (\d+) listed and (\d+) unlisted users on (\d+) servers/) {
136
		$result{'clients'} = $1 + $2 - 1; # don't count this script
137
		$result{'servers'} = $3;
138
	}
139
    # Or some recent ircnet ircd?
140
    elsif($s =~  /There are (\d+) users and \d+ services on (\d+) servers/) {
141
		$result{'clients'} = $1 - 1; # don't count this script
142
		$result{'servers'} = $2;
143
    }
144
    # Anything else goes here
145
    elsif($s =~  /There are (\d+) users and (\d+) invisible/) {
146
		$result{'clients'} = $1 + $2 - 1; # don't count this script
147
    }
148
    # And here (if there are no invisible count)
149
    elsif($s =~  /There are (\d+) users/) {
150
		$result{'clients'} = $1 - 1; # don't count this script
151
    }
152
	#printf "251 Got clients=%d servers=%d\n", ($result{'clients'} || -1), ($result{'servers'} || -1);
153
}
154

    
155
#irc_252:  'moo.us.p2p-network.net' '18 :operator(s) online' [18, operator(s) online]
156
# opers
157
sub irc_252 {
158
    my $sender = $_[SENDER];
159
    my $irc = $sender->get_heap();
160
	my $s = $_[ARG1];
161
    #print "In 252: $s\n";
162
    if($s =~  /^(\d+)/) {
163
		$result{'opers'} = $1;
164
    }
165
	#printf "254 Got channels %d\n", ($result{'channels'} || -1);
166
}
167

    
168
#irc_253:  'moo.us.p2p-network.net' '1 :unknown connection(s)' [1, unknown connection(s)]
169
sub irc_253 {
170
    my $sender = $_[SENDER];
171
    my $irc = $sender->get_heap();
172
	my $s = $_[ARG1];
173
    #print "In 253: $s\n";
174
    if($s =~  /^(\d+)/) {
175
		$result{'unknownconns'} = $1;
176
    }
177
	#printf "254 Got channels %d\n", ($result{'channels'} || -1);
178
}
179

    
180
#irc_254:  'moo.us.p2p-network.net' '1325 :channels formed' [1325, channels formed]
181
# luserchannels
182
sub irc_254 {
183
    my $sender = $_[SENDER];
184
    my $irc = $sender->get_heap();
185
	my $s = $_[ARG1];
186
    #print "In 254: $s\n";
187
    if($s =~  /^(\d+)/) {
188
		$result{'channels'} = $1;
189
    }
190
	#printf "254 Got channels %d\n", ($result{'channels'} || -1);
191
}
192

    
193
#irc_255:  'moo.us.p2p-network.net' 'I have 348 clients and 1 servers' [I have 348 clients and 1 servers]
194
# local clients/servers
195
sub irc_255 {
196
    my $sender = $_[SENDER];
197
    my $irc = $sender->get_heap();
198
	my $s = $_[ARG1];
199
    #print "In 255: $s\n";
200
	if($s =~  /I have (\d+) clients and (\d+) servers/) {
201
		$result{'localclients'} = $1-1; # don't count this script
202
		$result{'localservers'} = $2;
203
	}
204
}
205

    
206
#irc_265:  'moo.us.p2p-network.net' 'Current Local Users: 348  Max: 1900' [Current Local Users: 348  Max: 1900]
207
sub irc_265 {
208
    #print "In 265\n";
209
    my $sender = $_[SENDER];
210
    my $irc = $sender->get_heap();
211
	my $s = $_[ARG1];
212
    #print "In 265: $s\n";
213
	if($s =~  /Current Local Users: (\d+)\s+Max: (\d+)/) {
214
		$result{'localclients'} = $1-1; # don't count this script
215
		$result{'localclientmax'} = $2;
216
	}
217
}
218

    
219
#irc_266:  'moo.us.p2p-network.net' 'Current Global Users: 3552  Max: 8742' [Current Global Users: 3552  Max: 8742]
220
sub irc_266 {
221
    #print "In 266\n";
222
	my $sender = $_[SENDER];
223
    my $irc = $sender->get_heap();
224
	my $s = $_[ARG1];
225
    #print "In 266: $s\n";
226
	if($s =~  /Current Global Users: (\d+)\s+Max: (\d+)/) {
227
		$result{'clients'} = $1-1; # don't count this script
228
		$result{'clientmax'} = $2;
229
	}
230
}
231

    
232
# 372 motdline
233
sub irc_372 {
234
	return;
235
}
236
# 375 startofmotd
237
sub irc_375 {
238
	return;
239
}
240
# 376 endofmotd
241
sub irc_376 {
242
    my $sender = $_[SENDER];
243
    my $irc = $sender->get_heap();
244
	$irc->yield( quit => {} );
245
}
246

    
247
sub munin_print {
248
	my $key = shift;
249
	my $val = shift;
250
	print "${key}.value ".($val || 'U')."\n";
251
}
252

    
253
sub irc_disconnected {
254
	for my $var (qw(clients channels servers localclients clientmax localclientmax localservers opers unknownconns)) {
255
		munin_print($var, $result{$var});
256
	}
257
	exit 0;
258
}
259

    
260
sub irc_public {
261
    my ($sender, $who, $where, $what) = @_[SENDER, ARG0 .. ARG2];
262
    my $nick = ( split /!/, $who )[0];
263
    my $channel = $where->[0];
264

    
265
    if ( my ($rot13) = $what =~ /^rot13 (.+)/ ) {
266
        $rot13 =~ tr[a-zA-Z][n-za-mN-ZA-M];
267
        $irc->yield( privmsg => $channel => "$nick: $rot13" );
268
    }
269
    return;
270
}
271

    
272
# We registered for all events, this will produce some debug info.
273
sub _default {
274
    my ($event, $args) = @_[ARG0 .. $#_];
275
    my @output = ( "$event: " );
276

    
277
    for my $arg (@$args) {
278
        if ( ref $arg eq 'ARRAY' ) {
279
            push( @output, '[' . join(', ', @$arg ) . ']' );
280
        }
281
        else {
282
            push ( @output, "'$arg'" );
283
        }
284
    }
285
    print join ' ', @output, "\n";
286
    return 0;
287
}