Skip to content

Commit

Permalink
Add basic collector-implementation
Browse files Browse the repository at this point in the history
References #195

This is the first dumb-ass implementation of the collector-api.

It actually works fine for write AS IS, but has 0 safties. But I'm thinking the
API itself is reasonably stable and ready for testing.

I will need to extend it for dhcp-specific stuff, and the plan is to
create a secondary table with metadata (hidden from the user) and a few
other nice things.

Also, indexes. We need indexes. Obviously.
  • Loading branch information
KristianLyng committed Feb 13, 2019
1 parent 813ed7a commit 0bd0445
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 4 deletions.
113 changes: 109 additions & 4 deletions build/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ ALTER TABLE public.linknets_linknet_seq OWNER TO nms;
ALTER SEQUENCE public.linknets_linknet_seq OWNED BY public.linknets.linknet;


--
-- Name: metrics; Type: TABLE; Schema: public; Owner: postgres
--

CREATE TABLE public.metrics (
ts timestamp with time zone DEFAULT now(),
src text,
metadata jsonb,
data jsonb
);


ALTER TABLE public.metrics OWNER TO postgres;

--
-- Name: networks; Type: TABLE; Schema: public; Owner: nms
--
Expand Down Expand Up @@ -447,6 +461,13 @@ CREATE INDEX dhcp_network ON public.dhcp USING btree (network);
CREATE INDEX dhcp_time ON public.dhcp USING btree ("time");


--
-- Name: ping_brin_time; Type: INDEX; Schema: public; Owner: nms
--

CREATE INDEX ping_brin_time ON public.ping USING brin ("time");


--
-- Name: ping_index; Type: INDEX; Schema: public; Owner: nms
--
Expand All @@ -461,6 +482,20 @@ CREATE INDEX ping_index ON public.ping USING btree ("time");
CREATE INDEX ping_secondary_index ON public.ping_secondary_ip USING btree ("time");


--
-- Name: ping_switch_time_btree; Type: INDEX; Schema: public; Owner: nms
--

CREATE INDEX ping_switch_time_btree ON public.ping USING btree (switch, "time");


--
-- Name: ping_switch_time_unique_btree; Type: INDEX; Schema: public; Owner: nms
--

CREATE UNIQUE INDEX ping_switch_time_unique_btree ON public.ping USING btree (switch, "time");


--
-- Name: seen_mac_addr_family; Type: INDEX; Schema: public; Owner: nms
--
Expand All @@ -476,17 +511,66 @@ CREATE INDEX seen_mac_seen ON public.seen_mac USING btree (seen);


--
-- Name: snmp_time; Type: INDEX; Schema: public; Owner: nms
-- Name: snmp_brin_switch_time; Type: INDEX; Schema: public; Owner: nms
--

CREATE INDEX snmp_time ON public.snmp USING btree ("time");
CREATE INDEX snmp_brin_switch_time ON public.snmp USING brin (switch, "time");


--
-- Name: snmp_id_desc_btree; Type: INDEX; Schema: public; Owner: nms
--

CREATE INDEX snmp_id_desc_btree ON public.snmp USING btree (id DESC);


--
-- Name: snmp_time15; Type: INDEX; Schema: public; Owner: nms
-- Name: snmp_id_desc_switch_btree; Type: INDEX; Schema: public; Owner: nms
--

CREATE INDEX snmp_time15 ON public.snmp USING btree (id, switch);
CREATE INDEX snmp_id_desc_switch_btree ON public.snmp USING btree (id DESC, switch);


--
-- Name: snmp_id_switch_btree; Type: INDEX; Schema: public; Owner: nms
--

CREATE INDEX snmp_id_switch_btree ON public.snmp USING btree (id, switch);


--
-- Name: snmp_switch_btree; Type: INDEX; Schema: public; Owner: nms
--

CREATE INDEX snmp_switch_btree ON public.snmp USING btree (switch);


--
-- Name: snmp_switch_id_btree; Type: INDEX; Schema: public; Owner: nms
--

CREATE INDEX snmp_switch_id_btree ON public.snmp USING btree (switch, id);


--
-- Name: snmp_switch_id_desc_btree; Type: INDEX; Schema: public; Owner: nms
--

CREATE INDEX snmp_switch_id_desc_btree ON public.snmp USING btree (switch, id DESC);


--
-- Name: snmp_switch_time_btree; Type: INDEX; Schema: public; Owner: nms
--

CREATE INDEX snmp_switch_time_btree ON public.snmp USING btree (switch, "time");


--
-- Name: snmp_time; Type: INDEX; Schema: public; Owner: nms
--

CREATE INDEX snmp_time ON public.snmp USING btree ("time");


--
Expand All @@ -496,6 +580,20 @@ CREATE INDEX snmp_time15 ON public.snmp USING btree (id, switch);
CREATE INDEX snmp_time6 ON public.snmp USING btree ("time" DESC, switch);


--
-- Name: snmp_time_brin; Type: INDEX; Schema: public; Owner: nms
--

CREATE INDEX snmp_time_brin ON public.snmp USING brin ("time");


--
-- Name: snmp_unique_switch_time_btree; Type: INDEX; Schema: public; Owner: nms
--

CREATE UNIQUE INDEX snmp_unique_switch_time_btree ON public.snmp USING btree (switch, "time");


--
-- Name: switches_switch; Type: INDEX; Schema: public; Owner: nms
--
Expand Down Expand Up @@ -569,6 +667,13 @@ GRANT ALL ON TABLE public.linknet_ping TO dhcptail;
GRANT ALL ON TABLE public.linknets TO dhcptail;


--
-- Name: TABLE metrics; Type: ACL; Schema: public; Owner: postgres
--

GRANT ALL ON TABLE public.metrics TO nms;


--
-- Name: TABLE networks; Type: ACL; Schema: public; Owner: nms
--
Expand Down
23 changes: 23 additions & 0 deletions web/api/write/collector
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#! /usr/bin/perl
# vim:ts=8:sw=8
use lib '/opt/gondul/include';
use utf8;
use nms;
use nms::web qw($dbh db_safe_quote get_input finalize_output);

use strict;
use warnings;

my %input = %{JSON::XS::decode_json(get_input())};

my ($q,$check);
my $metric = $dbh->prepare("INSERT INTO metrics (src,metadata,data) VALUES(?,?,?)");

$nms::web::cc{'max-age'} = '0';
$nms::web::cc{'stale-while-revalidate'} = '0';
$nms::web::json{'state'} = 'ok';

foreach my $entry (@{$input{'data'}}) {
$metric->execute($input{'src'},JSON::XS::encode_json($input{'metadata'}),JSON::XS::encode_json($entry));
}
finalize_output();

0 comments on commit 0bd0445

Please sign in to comment.