vapix 0.1.0

Client for AXIS Communications devices' VAPIX API
Documentation
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! The VAPIX system log interface at `/axis-cgi/systemlog.cgi`.

use crate::*;
use chrono::prelude::*;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::str::FromStr;

/// A device's system log interface.
pub struct SystemLog<'a, T: Transport>(&'a Device<T>);

impl<'a, T: Transport> SystemLog<'a, T> {
    pub(crate) fn new(device: &'a Device<T>) -> Self {
        Self(device)
    }

    // Retrieve system log information.
    //
    // The level of information included in the log is set in the `Log.System` parameter group.
    pub async fn entries(&self) -> Result<Entries, Error<T::Error>> {
        let req = http::request::Builder::new()
            .method(http::Method::GET)
            .uri(self.0.uri_for("/axis-cgi/systemlog.cgi").unwrap())
            .body(Vec::new())
            .unwrap();

        let (resp, body) = self.0.roundtrip(req, "text/plain").await?;

        // Use the HTTP Date: header returned with the logs to help parse the log timestamps
        // If that's missing or un-parseable, use our system time
        // Clock drift isn't that big of a problem until we get to ±6 months.
        let now = resp
            .headers
            .get(http::header::DATE)
            .and_then(|v| v.to_str().ok())
            .and_then(|d| DateTime::parse_from_rfc2822(d).ok())
            .unwrap_or(Local::now().into());

        Ok(Entries::new(
            String::from_utf8_lossy(body.as_slice()).into_owned(),
            now,
        ))
    }
}

/// A set of system log entries returned from the API.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Entries {
    buffer: String,
    generated_at: DateTime<FixedOffset>,
}

impl Entries {
    /// Instantiate `Entries` from a response `String` and the timestamp at which it was produced
    /// by the API.
    ///
    /// `generated_at` is sourced from the HTTP `Date:` response header. It is used to help
    /// interpret certain incomplete timestamp formats.
    pub fn new(buffer: String, generated_at: DateTime<FixedOffset>) -> Self {
        Self {
            buffer,
            generated_at,
        }
    }

    /// Iterate over the `Entries`.
    pub fn iter(&self) -> EntriesIter {
        EntriesIter(self.buffer.rsplit('\n'), None, self.generated_at)
    }
}

/// An `Iterator` which parses `Entry` records.
pub struct EntriesIter<'a>(
    std::str::RSplit<'a, char>,
    Option<Timestamp>,
    DateTime<FixedOffset>,
);

impl<'a> Iterator for EntriesIter<'a> {
    type Item = Result<Entry<'a>, EntryParseError>;

    fn next(&mut self) -> Option<Self::Item> {
        // Parse the lines into a raw entries
        while let Some(line) = self.0.next() {
            // Trim trailing \r
            let line = if line.ends_with('\r') {
                &line[0..line.len() - 1]
            } else {
                line
            };

            let result = match line {
                _ if line.is_empty() => continue,
                _ if line.starts_with("----- ") && line.ends_with(" -----") => continue,
                line => RawEntry::parse(line),
            };

            return Some(
                result
                    .and_then(|raw_entry| raw_entry.cook(self.1, self.2))
                    .map(|entry| {
                        self.1 = Some(entry.timestamp);
                        entry
                    }),
            );
        }
        None
    }
}

impl<'a> IntoIterator for &'a Entries {
    type Item = Result<Entry<'a>, EntryParseError>;
    type IntoIter = EntriesIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Level {
    Emergency,
    Alert,
    Critical,
    Error,
    Warning,
    Notice,
    Info,
    Debug,
    Repeated,
}

impl AsRef<str> for Level {
    fn as_ref(&self) -> &str {
        match self {
            Level::Emergency => "[ EMERG   ]",
            Level::Alert => "[ ALERT   ]",
            Level::Critical => "[ CRIT    ]",
            Level::Error => "[ ERR     ]",
            Level::Warning => "[ WARNING ]",
            Level::Notice => "[ NOTICE  ]",
            Level::Info => "[ INFO    ]",
            Level::Debug => "[ DEBUG   ]",
            Level::Repeated => "[REPEATED ]",
        }
    }
}

impl std::fmt::Display for Level {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(self.as_ref())
    }
}

#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Source<'a> {
    None,
    Name(&'a str),
    NameAndPid(&'a str, u32),
}

impl<'a> Source<'a> {
    pub fn is_none(&self) -> bool {
        match self {
            Source::None => true,
            _ => false,
        }
    }

    pub fn is_some(&self) -> bool {
        match self {
            Source::None => false,
            _ => true,
        }
    }

    fn from_str(s: &'a str) -> Result<Self, ()> {
        match s.find('[') {
            Some(start) if s.ends_with(']') => {
                let name = &s[0..start];
                let pid = &s[start + 1..s.len() - 1];
                let pid = u32::from_str(pid).map_err(|_| ())?;
                Ok(Source::NameAndPid(name, pid))
            }
            _ if !s.is_empty() => Ok(Source::Name(s)),
            _ => Err(()),
        }
    }
}

impl<'a> std::fmt::Display for Source<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Source::None => Ok(()),
            Source::Name(s) => f.write_str(s),
            Source::NameAndPid(name, pid) => write!(f, "{}[{}]", name, pid),
        }
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Timestamp {
    Naive(NaiveDateTime),
    FixedOffset(DateTime<FixedOffset>),
}

impl std::fmt::Display for Timestamp {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Timestamp::Naive(dt) => dt.format("%Y-%m-%dT%H:%M:%S").fmt(f),
            Timestamp::FixedOffset(dt) => dt.format("%Y-%m-%dT%H:%M:%S%.3f%:z").fmt(f),
        }
    }
}

impl PartialOrd for Timestamp {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match (self, other) {
            (Timestamp::Naive(a), Timestamp::Naive(b)) => a.partial_cmp(b),
            (Timestamp::FixedOffset(a), Timestamp::FixedOffset(b)) => a.partial_cmp(b),
            _ => None,
        }
    }
}

mod raw_timestamp;
use raw_timestamp::RawTimestamp;

#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Entry<'a> {
    pub timestamp: Timestamp,
    pub hostname: &'a str,
    pub level: Level,
    pub source: Source<'a>,
    pub message: &'a str,
}

#[derive(Debug, Clone, PartialEq)]
struct RawEntry<'a> {
    pub timestamp: RawTimestamp,
    pub hostname: &'a str,
    pub level: Level,
    pub source: Source<'a>,
    pub message: &'a str,
}

#[derive(Debug, PartialEq)]
pub struct EntryParseError;

impl<'a> RawEntry<'a> {
    fn cook(
        self,
        successor: Option<Timestamp>,
        now: DateTime<FixedOffset>,
    ) -> Result<Entry<'a>, EntryParseError> {
        let Self {
            timestamp,
            hostname,
            level,
            source,
            message,
        } = self;

        let timestamp = timestamp.into_timestamp(successor, now)?;

        Ok(Entry {
            timestamp,
            hostname,
            level,
            source,
            message,
        })
    }

    fn parse(s: &'a str) -> Result<Self, EntryParseError> {
        Self::parse_old(s).or_else(|_| Self::parse_new(s))
    }

    fn parse_old(s: &'a str) -> Result<Self, EntryParseError> {
        if s.len() < 30 {
            return Err(EntryParseError);
        }

        let level = &s[0..11];
        let timestamp = &s[11..26];
        let rest = &s[27..];

        if &s[26..27] != " " {
            return Err(EntryParseError);
        }

        let level = match level {
            "<EMERG   > " => Level::Emergency,
            "<ALERT   > " => Level::Alert,
            "<CRITICAL> " => Level::Critical,
            "<ERR     > " => Level::Error,
            "<WARNING > " => Level::Warning,
            "<NOTICE  > " => Level::Notice,
            "<INFO    > " => Level::Info,
            "<DEBUG   > " => Level::Debug,
            "<REPEATED> " => Level::Repeated,
            _ => return Err(EntryParseError),
        };

        let timestamp = RawTimestamp::parse_old(timestamp)?;

        let (hostname, rest) = rest
            .find(' ')
            .map(|i| rest.split_at(i))
            .ok_or(EntryParseError)?;
        let rest = &rest[1..];

        let (source, message) = parse_rest(rest)?;

        Ok(Self {
            timestamp,
            hostname,
            level,
            source,
            message,
        })
    }

    fn parse_new(s: &'a str) -> Result<Self, EntryParseError> {
        let (timestamp, hostname, rest) = {
            let mut i = s.splitn(3, ' ');
            (
                i.next().ok_or(EntryParseError)?,
                i.next().ok_or(EntryParseError)?,
                i.next().ok_or(EntryParseError)?,
            )
        };

        let timestamp = RawTimestamp::parse_new(timestamp)?;

        if rest.len() < 13 {
            return Err(EntryParseError);
        }
        let (level, rest) = rest.split_at(12);
        let level = match level {
            "[ EMERG   ] " => Level::Emergency,
            "[ ALERT   ] " => Level::Alert,
            "[ CRIT    ] " => Level::Critical,
            "[ ERR     ] " => Level::Error,
            "[ WARNING ] " => Level::Warning,
            "[ NOTICE  ] " => Level::Notice,
            "[ INFO    ] " => Level::Info,
            "[ DEBUG   ] " => Level::Debug,
            _ => return Err(EntryParseError),
        };

        let (source, message) = parse_rest(rest)?;

        Ok(Self {
            timestamp,
            hostname,
            level,
            source,
            message,
        })
    }
}

fn parse_rest(rest: &str) -> Result<(Source, &str), EntryParseError> {
    // We might have "{source}: {message}", or we might just have "{message}".
    // Parse assuming we have both, and backtrack as needed.
    let (source, message) = match rest.find(": ").map(|i| rest.split_at(i)) {
        Some((source, message)) => match Source::from_str(source) {
            Ok(source) => (source, &message[2..]),
            Err(_) => (Source::None, rest),
        },
        None => (Source::None, rest),
    };

    Ok((source, message))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn entries() {
        crate::test_with_devices(|test_device| async move {
            let entries = test_device.device.system_log().entries().await?;
            let parsed = entries
                .iter()
                .collect::<Result<Vec<_>, _>>()
                .map_err(|_| Error::Other("couldn't parse an entry"))?;
            assert!(!parsed.is_empty());
            Ok(())
        })
    }

    fn partial_timestamp(month: u8, day: u8, hour: u32, minute: u32, second: u32) -> RawTimestamp {
        RawTimestamp::Partial(month, day, NaiveTime::from_hms(hour, minute, second))
    }

    fn full_timestamp(
        ymd: (i32, u32, u32),
        hmsms: (u32, u32, u32, u32),
        offset_secs: i32,
    ) -> RawTimestamp {
        let ymd = NaiveDate::from_ymd(ymd.0, ymd.1, ymd.2);
        let hms = NaiveTime::from_hms_milli(hmsms.0, hmsms.1, hmsms.2, hmsms.3);
        RawTimestamp::FixedOffset(
            FixedOffset::east(offset_secs)
                .from_local_datetime(&NaiveDateTime::new(ymd, hms))
                .unwrap(),
        )
    }

    #[test]
    fn parse_5_50_4_9() {
        assert_eq!(RawEntry::parse(
            "<REPEATED> Nov 14 06:08:29 axis-00408cb99b33 last CRITICAL  message repeated 4 times"
        ), Ok(RawEntry {
            timestamp: partial_timestamp(11, 14, 6, 8, 29),
            hostname: "axis-00408cb99b33",
            level: Level::Repeated,
            source: Source::None,
            message: "last CRITICAL  message repeated 4 times"
        }));

        assert_eq!(RawEntry::parse(
            "<CRITICAL> Nov 14 06:07:54 axis-00408cb99b33 kernel: CIFS VFS: Send error in SessSetup = -13"
        ), Ok(RawEntry {
            timestamp: partial_timestamp(11, 14, 6, 7, 54),
            hostname: "axis-00408cb99b33",
            level: Level::Critical,
            source: Source::Name("kernel"),
            message: "CIFS VFS: Send error in SessSetup = -13"
        }));
    }

    #[test]
    fn parse_5_51_7() {
        assert_eq!(
            RawEntry::parse(
                r"<INFO    > Oct  9 15:41:26 axis-00408cfb6888 syslogd[23459]: 1.4.1: restart."
            ),
            Ok(RawEntry {
                timestamp: partial_timestamp(10, 9, 15, 41, 26),
                hostname: "axis-00408cfb6888",
                level: Level::Info,
                source: Source::NameAndPid("syslogd", 23459),
                message: "1.4.1: restart.",
            })
        );
    }

    #[test]
    fn parse_9_80_2_2_entry() {
        assert_eq!(
            RawEntry::parse(
                "2020-10-09T10:30:02.425-05:00 axis-accc8ef7d108 [ INFO    ] systemd[1]: Started Rotate log files."
            ),
            Ok(RawEntry {
                timestamp: full_timestamp((2020, 10, 9), (10, 30, 2, 425), -5 * 3600),
                hostname: "axis-accc8ef7d108",
                level: Level::Info,
                source: Source::NameAndPid("systemd", 1),
                message: "Started Rotate log files."
            })
        );

        assert_eq!(
            RawEntry::parse(
                "2020-10-08T22:16:11.027-05:00 axis-accc8ef7d108 [ WARNING ] [    5.501068][    T1] systemd[1]: /usr/lib/systemd/system/imagectrl-data.service:2: Unknown key name \'Desription\' in section \'Unit\', ignoring."
            ),
            Ok(RawEntry {
                timestamp: full_timestamp((2020, 10, 8), (22, 16, 11, 27), -5 * 3600),
                hostname: "axis-accc8ef7d108",
                level: Level::Warning,
                source: Source::None,
                message:
                "[    5.501068][    T1] systemd[1]: /usr/lib/systemd/system/imagectrl-data.service:2: Unknown key name \'Desription\' in section \'Unit\', ignoring."
            })
        );

        assert_eq!(
            RawEntry::parse(
                "2020-10-08T22:16:11.033-05:00 axis-accc8ef7d108 [ WARNING ] kernel: [    7.050105][  T126] artpec_5: module license 'Proprietary' taints kernel."
            ),
            Ok(RawEntry {
                timestamp: full_timestamp((2020, 10, 8), (22, 16, 11, 33), -5 * 3600),
                hostname: "axis-accc8ef7d108",
                level: Level::Warning,
                source: Source::Name("kernel"),
                message:
                "[    7.050105][  T126] artpec_5: module license 'Proprietary' taints kernel."
            })
        );
    }
}