-
Notifications
You must be signed in to change notification settings - Fork 21
/
AmqpJson.pm
169 lines (140 loc) · 4.13 KB
/
AmqpJson.pm
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
package Collectd::Plugins::AmqpJson;
use strict;
use warnings;
use Collectd qw( :all );
use threads::shared;
use Net::RabbitMQ;
use JSON;
use Compress::Zlib;
=head1 NAME
Collectd::Plugins::AmqpJson - Send collectd metrics to AMQP in json format, based on Collectd::Plugins::Graphite by Joe Miller
=head1 VERSION
Version 1
=cut
our $VERSION = '1';
=head1 SYNOPSIS
This is a collectd plugin for sending collectd metrics to AMQP in json format.
In your collectd config:
<LoadPlugin "perl">
Globals true
</LoadPlugin>
<Plugin "perl">
BaseName "Collectd::Plugins"
LoadPlugin "AmqpJson"
<Plugin "AmqpJson">
Buffer "65507"
Prefix "datacenter"
Host "amqp.host"
Port "2003"
User "amqpuser"
Password "amqppass"
Exchange "exchangename"
VHost "/virtualhost"
Compression "On"
</Plugin>
</Plugin>
=head1 AUTHOR
Mark Steele, C<< <mark at control-alt-del.org> >>, original author of graphite plugin Joe Miller
=cut
my $buff :shared;
my $buffer_size = 8192;
my $prefix;
my $host = 'localhost';
my $port = 5672;
my $user;
my $password;
my $exchange;
my $vhost;
my $compress;
my $event_type = 'CollectdMetric';
sub amqp_json_config {
my ($ci) = @_;
foreach my $item (@{$ci->{'children'}}) {
my $key = lc($item->{'key'});
my $val = $item->{'values'}->[0];
if ($key eq 'buffer' ) {
$buffer_size = $val;
} elsif ($key eq 'prefix' ) {
$prefix = $val;
} elsif ($key eq 'host') {
$host = $val;
} elsif ($key eq 'port') {
$port = $val;
} elsif ($key eq 'user') {
$user = $val;
} elsif ($key eq 'password') {
$password = $val;
} elsif ($key eq 'exchange') {
$exchange = $val;
} elsif ($key eq 'vhost') {
$vhost = $val;
} elsif ($key eq 'compression' && lc($val) eq 'on') {
$compress = 1;
} elsif ($key eq 'eventtype') {
$event_type = $val;
}
}
return 1;
}
sub amqp_json_write {
my ($type, $ds, $vl) = @_;
my $host = $vl->{'host'};
$host =~ s/\./_/g;
my $hashtemplate = {};
$hashtemplate->{'plugin'} = $vl->{'plugin'};
$hashtemplate->{'type'} = $vl->{'type'};
if ( defined $vl->{'plugin_instance'} ) {
$hashtemplate->{'plugin_instance'} = $vl->{'plugin_instance'};
}
if ( defined $vl->{'type_instance'} ) {
$hashtemplate->{'type_instance'} = $vl->{'type_instance'};
}
my $bufflen;
{
lock($buff);
for (my $i = 0; $i < scalar (@$ds); ++$i) {
my $hashref = $hashtemplate;
$hashref->{'name'} = $ds->[$i]->{'name'};
$hashref->{'value'} = $vl->{'values'}->[$i];
$hashref->{'time'} = $vl->{'time'};
$hashref->{'datacenter'} = $prefix;
$hashref->{'host'} = $host;
$hashref->{'event_type'} = $event_type;
$buff .= encode_json($hashref) . "\n";
}
$bufflen = length($buff);
}
if ( $bufflen >= $buffer_size ) {
send_to_amqp();
}
return 1;
}
sub send_to_amqp {
lock($buff);
return 0 if !length($buff);
my $mq = Net::RabbitMQ->new();
eval { $mq->connect($host , { port => $port, user => $user, password => $password, vhost => $vhost }); };
if ($@ eq '') {
eval {
$mq->channel_open(1);
$mq->exchange_declare(1, $exchange, { 'exchange_type' => 'topic', 'durable' => 1, 'auto_delete' => 0 });
$mq->publish(1, '', $compress ? compress($buff) : $buff, { exchange => $exchange });
$mq->disconnect();
};
if ($@ ne '') {
plugin_log(LOG_ERR, "AmqpJson.pm: error publishing to amqp, losing data: " . $@);
}
} else {
plugin_log(LOG_ERR, "AmqpJson.pm: failed to connect to amqp, losing data");
}
$buff = '';
return 1;
}
sub amqp_json_flush {
send_to_amqp();
return 1;
}
plugin_register (TYPE_CONFIG, "AmqpJson", "amqp_json_config");
plugin_register (TYPE_WRITE, "AmqpJson", "amqp_json_write");
plugin_register (TYPE_FLUSH, "AmqpJson", "amqp_json_flush");
1; # End of Collectd::Plugins::AmqpJson