Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
3241 rexy 1
<?php
2
 
3
namespace mbolli\nfsen_ng\datasources;
4
 
5
use mbolli\nfsen_ng\common\Config;
6
use mbolli\nfsen_ng\common\Debug;
7
 
8
class Akumuli implements Datasource {
9
    private readonly Debug $d;
10
    private $client;
11
 
12
    public function __construct() {
13
        $this->d = Debug::getInstance();
14
        $this->connect();
15
    }
16
 
17
    /**
18
     * connects to TCP socket.
19
     */
20
    public function connect(): void {
21
        try {
22
            $this->client = stream_socket_client('tcp://' . Config::$cfg['db']['akumuli']['host'] . ':' . Config::$cfg['db']['akumuli']['port'], $errno, $errmsg);
23
 
24
            if ($this->client === false) {
25
                throw new \Exception('Failed to connect to Akumuli: ' . $errmsg);
26
            }
27
        } catch (\Exception $e) {
28
            $this->d->dpr($e);
29
        }
30
    }
31
 
32
    /**
33
     * Convert data to redis-compatible string and write to Akumuli.
34
     */
35
    public function write(array $data): bool {
36
        $fields = array_keys($data['fields']);
37
        $values = array_values($data['fields']);
38
 
39
        // writes assume redis protocol. first byte identification:
40
        // "+" simple strings  "-" errors  ":" integers  "$" bulk strings  "*" array
41
        $query = '+' . implode('|', $fields) . ' source=' . $data['source'] . "\r\n"
42
            . '+' . $data['date_iso'] . "\r\n" // timestamp
43
            . '*' . \count($fields) . "\r\n"; // length of following array
44
 
45
        // add the $values corresponding to $fields
46
        foreach ($values as $v) {
47
            $query .= ':' . $v . "\r\n";
48
        }
49
 
50
        $this->d->dpr([$query]);
51
 
52
        // write redis-compatible string to socket
53
        fwrite($this->client, $query);
54
 
55
        return stream_get_contents($this->client);
56
 
57
        // to read:
58
        // curl localhost:8181/api/query -d "{'select':'flows'}"
59
    }
60
 
61
    public function __destruct() {
62
        if (\is_resource($this->client)) {
63
            fclose($this->client);
64
        }
65
    }
66
 
67
    /**
68
     * Gets data for plotting the graph in the frontend.
69
     * Each row in $return['data'] will be one line in the graph.
70
     * The lines can be
71
     *   * protocols - $sources must not contain more than one source (legend e.g. gateway_flows_udp, gateway_flows_tcp)
72
     *   * sources - $protocols must not contain more than one protocol (legend e.g. gateway_traffic_icmp, othersource_traffic_icmp).
73
     *
74
     * @param int    $start     timestamp
75
     * @param int    $end       timestamp
76
     * @param array  $sources   subset of sources specified in settings
77
     * @param array  $protocols UDP/TCP/ICMP/other
78
     * @param string $type      flows/packets/traffic
79
     *
80
     * @return array in the following format:
81
     *
82
     * $return = array(
83
     *  'start' => 1490484600,      // timestamp of first value
84
     *  'end' => 1490652000,        // timestamp of last value
85
     *  'step' => 300,              // resolution of the returned data in seconds. lowest value would probably be 300 = 5 minutes
86
     *  'data' => array(
87
     *      0 => array(
88
     *          'legend' => 'source_type_protocol',
89
     *          'data' => array(
90
     *          1490484600 => 33.998333333333,
91
     *          1490485200 => 37.005, ...
92
     *          )
93
     *      ),
94
     *      1 => array( e.g. gateway_flows_udp ...)
95
     *  )
96
     * );
97
     */
98
    public function get_graph_data(int $start, int $end, array $sources, array $protocols, array $ports, string $type = 'flows', string $display = 'sources'): array|string {
99
        // TODO: Implement get_graph_data() method.
100
        return [];
101
    }
102
 
103
    /**
104
     * Gets the timestamps of the first and last entry in the datasource (for this specific source).
105
     *
106
     * @return array (timestampfirst, timestamplast)
107
     */
108
    public function date_boundaries(string $source): array {
109
        // TODO: Implement date_boundaries() method.
110
        return [];
111
    }
112
 
113
    /**
114
     * Gets the timestamp of the last update of the datasource (for this specific source).
115
     */
116
    public function last_update(string $source, int $port = 0): int {
117
        // TODO: Implement last_update() method.
118
        return 0;
119
    }
120
 
121
    /**
122
     * Gets the path where the datasource's data is stored.
123
     */
124
    public function get_data_path(): string {
125
        // TODO: Implement get_data_path() method.
126
        return '';
127
    }
128
 
129
    /**
130
     * Removes all existing data for every source in $sources.
131
     * If $sources is empty, remove all existing data.
132
     */
133
    public function reset(array $sources): bool {
134
        // TODO: Implement reset() method.
135
        return true;
136
    }
137
}