summaryrefslogtreecommitdiff
path: root/src/collectl2pcp/timestamp.c
blob: 45de17b463a97e11952dca71f1bb6bd28cf43084 (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
/*
 * Copyright (c) 2013 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * Handler for timestamps
 * >>> 1368076410.001 <<<
 */

#include "metrics.h"

static int seconds = 0, mseconds = 0;

int
timestamp_flush(void)
{
    int sts;
    int err = 0;

    /*
     * timstamps in collectl logs are seconds.mseconds since the epoch in utc
     * Since we've set pmiSetTimezone to what was found in the header, we need
     * to offset it here so the PCP archive matches the host timezone.
     */
    if (seconds && (sts = pmiWrite(seconds + utc_offset * 60 * 60, mseconds*1000)) < 0) {
	if (sts != PMI_ERR_NODATA) {
	    fprintf(stderr, "Error: pmiWrite failed: error %d: %s\n", sts, pmiErrStr(sts));
	    err = sts; /* probably fatal */
	}
    }

    return err;
}

int
timestamp_handler(handler_t *h, fields_t *f)
{

    int sts;
    int err = 0;

    /* >>> 1368076390.001 <<< */
    if (f->nfields != 3)
    	return -1;
    if ((sts = timestamp_flush()) < 0)
    	err = sts;
    sscanf(f->fields[1], "%d.%d", &seconds, &mseconds);

    return err;
}