Skip to content

Commit b584127

Browse files
author
Anze
committed
Use redis for saving previous counter values
1 parent d8be96b commit b584127

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ansicolors = "*"
1515
easysnmp = {editable = true,git = "http://github.com/grafolean/easysnmp"}
1616
mathjspy = "*"
1717
numpy = "*"
18+
redis = "*"
1819

1920
[requires]
2021
python_version = "3.6"

Pipfile.lock

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.dev.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,12 @@ services:
3232
# on the same host, '127.0.0.1' translates to container, not host - this directive changes network mode
3333
# so that Docker networking is bypassed.
3434
network_mode: "host"
35+
36+
redis:
37+
image: redis:5-alpine
38+
container_name: grafolean-collector-snmp-redis
39+
ports:
40+
- "127.0.0.1:6379:6379"
41+
# We advise not to use `network_mode: "host"` in production, because it would expose Redis to host network
42+
# (even if access is limited to 127.0.0.1).
43+
network_mode: "host"

docker-compose.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ services:
1616
environment:
1717
# Backend url must be set to the address of the Grafolean backend, for example this uses Grafolean hosted service:
1818
# - BACKEND_URL=https://grafolean.com/api
19+
# IMPORTANT: '127.0.0.1' and 'localhost' are _never_ correct addresses for Grafolean backend, because they translate
20+
# to container, not host.
1921
- BACKEND_URL=
2022
# To use SNMP Collector, a bot with the protocol "snmp" must be added via user interface, then the token needs to be copied here:
2123
- BOT_TOKEN=
2224
# Interval between fetching information about jobs:
2325
- JOBS_REFRESH_INTERVAL=60
26+
- REDIS_HOST=redis
2427
restart: always
25-
# Grafolean backend must be accessible on BACKEND_URL *from the point of view of container*. If running
26-
# on the same host, '127.0.0.1' translates to container, not host - this directive changes network mode
27-
# so that Docker networking is bypassed.
28-
network_mode: "host"
28+
29+
redis:
30+
image: redis:5-alpine
31+
container_name: grafolean-collector-snmp-redis

snmpcollector.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pytz import utc
77
from colors import color
88
import requests
9+
import redis
910

1011
from easysnmp import Session, SNMPVariable
1112
from mathjspy import MathJS
@@ -26,18 +27,19 @@ class NoValueForOid(Exception):
2627
pass
2728

2829

29-
previous_counter_values = {}
30+
REDIS_HOST = os.environ.get('REDIS_HOST', '127.0.0.1')
31+
r = redis.Redis(host=REDIS_HOST)
3032

3133

3234
def _get_previous_counter_value(counter_ident):
33-
prev_value = previous_counter_values.get(counter_ident)
34-
if prev_value is None:
35+
prev_value = r.hgetall(counter_ident)
36+
if not prev_value: # empty dict
3537
return None, None
36-
return prev_value
38+
return int(float(prev_value[b'v'])), float(prev_value[b't'])
3739

3840

3941
def _save_current_counter_value(new_value, now, counter_ident):
40-
previous_counter_values[counter_ident] = (new_value, now)
42+
r.hmset(counter_ident, {b'v': new_value, b't': now})
4143

4244

4345
def _convert_counters_to_values(results, now, counter_ident_prefix):

0 commit comments

Comments
 (0)