aboutsummaryrefslogtreecommitdiff
path: root/rrd/models.py
blob: 7ccc89aa74a298944b2cc03db04ee2191e7632b7 (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
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
170
import logging
import os
import pathlib

import django.contrib.auth.models as amodels
import rrdtool
from django.conf import settings
from django.db import models

log = logging.getLogger(__name__)


def _sanitize_path(path):
    # changing all separators to a _ should result in an ugly filename
    # that will not cause issues.
    # an additional _ is added at the beginning to prevent hidden files
    for sep in os.sep, os.altsep:
        if sep:
            path = path.replace(sep, "_")
    return "_" + path


class ModelWithPerms(models.Model):
    users_read = models.ManyToManyField(
        amodels.User,
        related_name="%(class)s_read",
        blank=True,
    )
    users_write = models.ManyToManyField(
        amodels.User,
        related_name="%(class)s_write",
        blank=True,
    )
    groups_read = models.ManyToManyField(
        amodels.Group,
        related_name="%(class)s_read",
        blank=True,
    )
    groups_write = models.ManyToManyField(
        amodels.Group,
        related_name="%(class)s_write",
        blank=True,
    )

    class Meta:
        abstract = True


class DataSource(ModelWithPerms):
    # an mqtt topic can be as long as 65,535 bytes when UTF-8 encoded,
    # which is probably too much for a sensible db
    topic = models.CharField(max_length=512)
    path = models.CharField(
        max_length=512,
    )
    rrd_config = models.TextField(
        default=settings.RRD_DS_CONFIG
    )
    active = models.BooleanField(
        default=True,
    )

    def __str__(self):
        return self.topic

    @property
    def rrd_path(self):
        path = os.path.abspath(os.path.join(
            settings.RRD_DB_PATH, self.path
        ))
        base_path = os.path.abspath(settings.RRD_DB_PATH)
        if pathlib.Path(base_path) not in pathlib.Path(path).parents:
            return os.path.abspath(os.path.join(
                settings.RRD_DB_PATH,
                _sanitize_path(self.path)
            ))
        return path

    @property
    def ds_name(self):
        return self.topic.split("/")[-1]

    @property
    def lastupdate(self):
        try:
            last = rrdtool.lastupdate(self.rrd_path)
        except rrdtool.OperationalError as e:
            log.warning("Failure reading from ds: %s", e)
            return (None, None)
        else:
            return last["date"], last["ds"][self.ds_name]

    def update(self, ts, value):
        if not os.path.isfile(self.rrd_path):
            rrdtool.create(
                self.rrd_path,
                "--no-overwrite",
                self.rrd_config.format(
                    ds_name=self.ds_name
                ).strip().split('\n'),
            )
        try:
            rrdtool.update(
                self.rrd_path,
                str(ts) + ":" + str(value)
            )
        except ValueError as e:
            log.warning("Could not update ds: %s", e)

        for graph in self.graph_set.all():
            graph.update()


class Graph(ModelWithPerms):
    title = models.CharField(max_length=64)
    data_sources = models.ManyToManyField(DataSource)
    path = models.CharField(
        max_length=512,
    )
    rrd_config = models.TextField(
        default=settings.RRD_GRAPH_CONFIG
    )

    def __str__(self):
        return self.title

    @property
    def graph_path(self):
        path = os.path.abspath(os.path.join(
            settings.RRD_GRAPH_PATH, self.path
        ))
        base_path = os.path.abspath(settings.RRD_GRAPH_PATH)
        if pathlib.Path(base_path) not in pathlib.Path(path).parents:
            return os.path.abspath(os.path.join(
                settings.RRD_GRAPH_PATH,
                _sanitize_path(self.path)
            ))
        return path

    def update(self):
        graph_path = self.graph_path
        os.makedirs(os.path.dirname(graph_path), exist_ok=True)
        rrd_paths = []
        rrd_topics = []
        rrd_ds_names = []
        for ds in self.data_sources.all():
            rrd_paths.append(ds.rrd_path)
            rrd_topics.append(ds.topic)
            rrd_ds_names.append(ds.ds_name)
        opts = self.rrd_config.format(
            topics=rrd_topics,
            ds_names=rrd_ds_names,
            ds_paths=rrd_paths,
            title=self.title,
        ).strip().split('\n')
        opts = [o.strip() for o in opts]
        rrdtool.graph(
            graph_path,
            * opts
        )


class Dashboard(ModelWithPerms):
    title = models.CharField(max_length=64)
    graphs = models.ManyToManyField(Graph)
    data_sources = models.ManyToManyField(DataSource)
    template = models.TextField()

    def __str__(self):
        return self.title