Skip to content

Commit 2a32dd8

Browse files
committed
Prometheus, InfluxDB, Copyright
- Fix #58: Add Prometheus output - InfluxDB: fix timestamp option - Update Copyright year
1 parent 41cc86b commit 2a32dd8

File tree

15 files changed

+201
-14
lines changed

15 files changed

+201
-14
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
DSC DataTool
22

3-
Copyright (c) 2016-2020 OARC, Inc.
3+
Copyright (c) 2016-2022 OARC, Inc.
44
All rights reserved.
55

66
Redistribution and use in source and binary forms, with or without

debian/copyright

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ Upstream-Name: dsc-datatool
33
Source: https://github.com/DNS-OARC/dsc-datatool
44

55
Files: *
6-
Copyright: 2016-2020 OARC, Inc.
6+
Copyright: 2016-2022 OARC, Inc.
77
License: BSD-3-Clause
88

99
Files: debian/*
10-
Copyright: 2020 Jerry Lundström <lundstrom.jerry@gmail.com>
10+
Copyright: 2022 Jerry Lundström <lundstrom.jerry@gmail.com>
1111
License: BSD-3-Clause
1212

1313
License: BSD-3-Clause

debian/manpages

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ man/man7/dsc-datatool-transformer-reranger.7
22
man/man7/dsc-datatool-generator-client_subnet_country.7
33
man/man7/dsc-datatool-generator-client_subnet_authority.7
44
man/man7/dsc-datatool-output-influxdb.7
5+
man/man7/dsc-datatool-output-prometheus.7
56
man/man7/dsc-datatool-transformer-labler.7
67
man/man7/dsc-datatool-transformer-netremap.7
78
man/man5/dsc-datatool.conf.5

dsc_datatool/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ExampleOutput(Output):
2929
def process(self, datasets)
3030
...
3131
32-
:copyright: 2020 OARC, Inc.
32+
:copyright: 2022 OARC, Inc.
3333
"""
3434

3535
__version__ = '1.0.2'

dsc_datatool/generator/client_subnet_authority.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
Part of dsc_datatool.
66
7-
:copyright: 2020 OARC, Inc.
7+
:copyright: 2022 OARC, Inc.
88
"""
99

1010
import csv

dsc_datatool/generator/client_subnet_country.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
Part of dsc_datatool.
66
7-
:copyright: 2020 OARC, Inc.
7+
:copyright: 2022 OARC, Inc.
88
"""
99

1010
import maxminddb

dsc_datatool/input/dat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
Part of dsc_datatool.
66
7-
:copyright: 2020 OARC, Inc.
7+
:copyright: 2022 OARC, Inc.
88
"""
99

1010
import re

dsc_datatool/input/xml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
Part of dsc_datatool.
66
7-
:copyright: 2020 OARC, Inc.
7+
:copyright: 2022 OARC, Inc.
88
"""
99

1010
import logging

dsc_datatool/output/influxdb.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
55
Part of dsc_datatool.
66
7-
:copyright: 2020 OARC, Inc.
7+
:copyright: 2022 OARC, Inc.
88
"""
99

1010
import re
1111
import sys
1212
import atexit
1313

14-
from dsc_datatool import Output,args
14+
from dsc_datatool import Output, args
1515

1616

1717
_re = re.compile(r'([,=\s])')
@@ -60,7 +60,7 @@ def __init__(self, opts):
6060
if timestamp == 'start':
6161
pass
6262
elif timestamp == 'stop':
63-
self.timestamp = False
63+
self.start_timestamp = False
6464
else:
6565
raise Exception('timestamp option invalid')
6666
file = opts.get('file', None)

dsc_datatool/output/prometheus.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
"""dsc_datatool.output.prometheus
2+
3+
See `man dsc-datatool-output prometheus`.
4+
5+
Part of dsc_datatool.
6+
7+
:copyright: 2022 OARC, Inc.
8+
"""
9+
10+
import re
11+
import sys
12+
import atexit
13+
14+
from dsc_datatool import Output, args
15+
16+
17+
_re = re.compile(r'([\\\n"])')
18+
19+
20+
def _key(key):
21+
return re.sub(_re, r'\\\1', key)
22+
23+
24+
def _val(val):
25+
ret = re.sub(_re, r'\\\1', val)
26+
if ret == '':
27+
return '""'
28+
return '"%s"' % ret
29+
30+
31+
class Prometheus(Output):
32+
show_timestamp = True
33+
start_timestamp = True
34+
fh = None
35+
type_def = ""
36+
type_printed = False
37+
38+
39+
def __init__(self, opts):
40+
Output.__init__(self, opts)
41+
timestamp = opts.get('timestamp', 'start')
42+
if timestamp == 'hide':
43+
self.show_timestamp = False
44+
elif timestamp == 'start':
45+
pass
46+
elif timestamp == 'stop':
47+
self.start_timestamp = False
48+
else:
49+
raise Exception('timestamp option invalid')
50+
file = opts.get('file', None)
51+
append = opts.get('append', False)
52+
if file:
53+
if append:
54+
self.fh = open(file, 'a')
55+
else:
56+
self.fh = open(file, 'w')
57+
atexit.register(self.close)
58+
else:
59+
self.fh = sys.stdout
60+
61+
62+
def close(self):
63+
if self.fh:
64+
self.fh.close()
65+
self.fh = None
66+
67+
68+
def _process(self, tags, timestamp, dimension, fh):
69+
if dimension.dimensions is None:
70+
return
71+
72+
if len(dimension.dimensions) > 0:
73+
if not (dimension.name == 'All' and dimension.value == 'ALL'):
74+
tags += ',%s=%s' % (_key(dimension.name.lower()), _val(dimension.value))
75+
for d2 in dimension.dimensions:
76+
self._process(tags, timestamp, d2, fh)
77+
return
78+
79+
if dimension.values is None:
80+
return
81+
82+
if len(dimension.values) > 0:
83+
tags += ',%s=' % _key(dimension.name.lower())
84+
85+
for k, v in dimension.values.items():
86+
if not self.type_printed:
87+
print(self.type_def, file=fh)
88+
self.type_printed = True
89+
if self.show_timestamp:
90+
print('%s%s} %s %s' % (tags, _val(k), v, timestamp), file=fh)
91+
else:
92+
print('%s%s} %s' % (tags, _val(k), v), file=fh)
93+
94+
95+
def process(self, datasets):
96+
for dataset in datasets:
97+
self.type_def = '# TYPE %s gauge' % _key(dataset.name.lower())
98+
self.type_printed = False
99+
tags = '%s{server=%s,node=%s' % (_key(dataset.name.lower()), _val(args.server), _val(args.node))
100+
if self.start_timestamp:
101+
timestamp = dataset.start_time * 1000
102+
else:
103+
timestamp = dataset.end_time * 1000
104+
105+
for d in dataset.dimensions:
106+
self._process(tags, timestamp, d, self.fh)
107+
108+
109+
if sys.version_info[0] == 3 and sys.version_info[1] == 5: # pragma: no cover
110+
Output.__init_subclass__(Prometheus)

0 commit comments

Comments
 (0)