summaryrefslogtreecommitdiff
path: root/bin/mqtt-to-influxdb
blob: 652e53f925e900fd7d1a2c50de5237e67bf4fe91 (plain)
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
#!/usr/bin/env perl

use strict;
use warnings;
use 5.020;

our $VERSION = '0.00';

use File::Slurp qw(read_file);
use Net::MQTT::Simple;
use LWP::UserAgent;
use YAML::XS;

sub load_config {
	my ($config_file) = @_;
	my $content       = read_file($config_file);
	my $yaml          = Load($content);
	return $yaml;
}

my ($config_file) = @ARGV;

if ( not defined $config_file ) {
	die("Usage: $0 <config.yaml>\n");
}

my $user_config = load_config($config_file);
my %config = (
	influxdb => $user_config->{influxdb},
	mqtt => $user_config->{mqtt},
);

for my $subscription ( @{ $user_config->{subscriptions} } ) {
	$config{subscription}{ $subscription->{topic} } = {};
	for my $key (qw(attributes key norm_factor)) {
		if ( exists $subscription->{$key} ) {
			$config{subscription}{ $subscription->{topic} }{$key} = $subscription->{$key};
		}
	}
}

if ( not defined $config{mqtt}{server} ) {
	die("Error: configuration must specify an mqtt server\n");
}
if ( not defined $config{influxdb}{server} ) {
	die("Error: configuration must specify an influxdb server\n");
}
if ( not defined $config{influxdb}{database} ) {
	die("Error: configuration must specify a database\n");
}

my $mqtt = Net::MQTT::Simple->new($config{mqtt}{server});
my $ua = LWP::UserAgent->new(timeout => 10);
my $api_endpoint = sprintf('http://%s:%s/write?db=%s',
	$config{influxdb}{server}, $config{influxdb}{port}, $config{influxdb}{database});

my %subscription;

sub message_handler {
	my ($subscription, $topic, $message) = @_;

	my @topic_parts = split(qr{ / }x, $topic);
	my $key = $config{subscription}{$subscription}{key} // $topic;
	my $norm_factor = $config{subscription}{$subscription}{norm_factor} // 1;
	my $attributes = $config{subscription}{$subscription}{attributes} // {};
	my @message_and_tags = ($key);

	for my $attribute (keys %{$attributes}) {
		my $attribute_value = $attributes->{$attribute};
		my $tag_key = $attribute;
		my $tag_value = $attribute_value;

		if ($tag_value =~ m{ ^ \$ (?<topic_index> \d+ ) $ }x) {
			$tag_value = $topic_parts[ $+{topic_index} - 1 ];
		}

		push(@message_and_tags, "${tag_key}=${tag_value}");
	}

	my $api_msg = sprintf('%s value=%f',
		join(',', @message_and_tags),
		$message * $norm_factor);

	say $api_msg;

	$ua->post($api_endpoint, Content => $api_msg);
}

for my $name ( keys %{$config{subscription}} ) {
	$subscription{$name} = sub {
		my ($topic, $message) = @_;
		message_handler($name, $topic, $message);
	};
}

$mqtt->run(%subscription);

__END__

=head1 NAME

=head1 SYNOPSIS

=head1 VERSION

=head1 DESCRIPTION

=head1 OPTIONS

=over

=back

=head1 EXIT STATUS

=head1 CONFIGURATION

None.

=head1 DEPENDENCIES

=over

=back

=head1 BUGS AND LIMITATIONS

=head1 AUTHOR

Copyright (C) 2017 by Daniel Friesel E<lt>derf@finalrewind.orgE<gt>

=head1 LICENSE

  0. You just DO WHAT THE FUCK YOU WANT TO.