summaryrefslogtreecommitdiff
path: root/tools/rrdbot-create.c
blob: 22dbaa49524bb0b3200bc75fff744e53fe91b26d (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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
/*
 * Copyright (c) 2005, Nate Nielsen
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above
 *       copyright notice, this list of conditions and the
 *       following disclaimer.
 *     * Redistributions in binary form must reproduce the
 *       above copyright notice, this list of conditions and
 *       the following disclaimer in the documentation and/or
 *       other materials provided with the distribution.
 *     * The names of contributors to this software may not be
 *       used to endorse or promote products derived from this
 *       software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 *
 * CONTRIBUTORS
 *  Nate Nielsen <nielsen@memberwebs.com>
 *
 */

#include "usuals.h"
#include <errno.h>
#include <unistd.h>
#include <stdarg.h>
#include <err.h>
#include <sys/stat.h>
#include <rrd.h>

#include "config-parser.h"

/* -----------------------------------------------------------------------------
 * CONSTANTS
 */

/* The default command line options */
#define DEFAULT_CONFIG      CONF_PREFIX "/rrdbot"
#define DEFAULT_WORK        "/var/db/rrdbot"

#define CONFIG_CREATE   "create"
#define CONFIG_GENERAL  "general"
#define CONFIG_POLL     "poll"
#define CONFIG_INTERVAL "interval"
#define CONFIG_ARCHIVE  "archive"
#define CONFIG_TYPE     "type"
#define CONFIG_MIN      "min"
#define CONFIG_MAX      "max"
#define CONFIG_CF       "cf"
#define CONFIG_RRD      "rrd"

#define VAL_UNKNOWN     "U"
#define VAL_ABSOLUTE    "ABSOLUTE"
#define VAL_GAUGE       "GAUGE"
#define VAL_COUNTER     "COUNTER"
#define VAL_DERIVE      "DERIVE"
#define VAL_AVERAGE     "AVERAGE"
#define VAL_MIN         "MIN"
#define VAL_MAX         "MAX"
#define VAL_LAST        "LAST"

#define VAL_SECOND      "second"
#define VAL_SECONDS     "seconds"
#define VAL_SECONDLY    "secondly"
#define VAL_MINUTE      "minute"
#define VAL_MINUTES     "minutes"
#define VAL_MINUTELY    "minutely"
#define VAL_HOUR        "hour"
#define VAL_HOURS       "hours"
#define VAL_HOURLY      "hourly"
#define VAL_DAY         "day"
#define VAL_DAYS        "days"
#define VAL_DAILY       "daily"
#define VAL_WEEK        "week"
#define VAL_WEEKS       "weeks"
#define VAL_WEEKLY      "weekly"
#define VAL_MONTH       "month"
#define VAL_MONTHS      "months"
#define VAL_MONTHLY     "monthly"
#define VAL_YEAR        "year"
#define VAL_YEARS       "years"
#define VAL_YEARLY      "yearly"

#define FIELD_VALID     "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-0123456789."

/* -----------------------------------------------------------------------------
 * DECLARATIONS
 */

typedef struct _field_arg
{
    const char *name;
    const char *dst;
    const char *min;
    const char *max;

    struct _field_arg* next;
}
field_arg;

typedef struct _rra_arg
{
    uint num;
    uint per;
    uint total;

    struct _rra_arg* next;
}
rra_arg;

typedef struct _create_arg
{
    char buf[256];
    struct _create_arg* next;
}
create_arg;

typedef struct _create_ctx
{
    const char* workdir;
    const char* confname;
    const char* rrdname;
    uint interval;
    const char *cf;
    int create;
    int skip;

    field_arg* fields;
    rra_arg* rras;
}
create_ctx;

/* Wether to print out status updates */
static int g_verbose = 0;
static int g_print = 0;

/* -----------------------------------------------------------------------------
 * HELPERS
 */

void
verb(const char* fmt, ...)
{
    va_list va;

    if(!g_verbose)
        return;

    va_start(va, fmt);
    vwarnx(fmt, va);
    va_end(va);
}

static field_arg*
field_for(create_ctx* ctx, char* name)
{
    field_arg* field;

    for(field = ctx->fields; field; field = field->next) {
        if (strcmp(name, field->name) == 0)
            return field;
    }

    field = (field_arg*)calloc(1, sizeof(field_arg));
    if(!field)
        errx(1, "out of memory");

    field->name = name;
    field->dst = VAL_ABSOLUTE;
    field->min = VAL_UNKNOWN;
    field->max = VAL_UNKNOWN;

    field->next = ctx->fields;
    ctx->fields = field;

    return field;
}

static void
context_reset(create_ctx* ctx)
{
    field_arg* field;
    rra_arg* rra;

    while(ctx->fields) {
        field = ctx->fields->next;
        free(ctx->fields);
        ctx->fields = field;
    }

    while(ctx->rras) {
        rra = ctx->rras->next;
        free(ctx->rras);
        ctx->rras = rra;
    }

    ctx->confname = NULL;
    ctx->cf = VAL_AVERAGE;
    ctx->interval = 0;

    ctx->create = 0;
    ctx->skip = 0;
}

/* -----------------------------------------------------------------------------
 * CREATE
 */

static int
mkdir_p(char* path)
{
    struct stat sb;
    int first, last, retval = 0;
    char* p = path;

    /* Skip leading '/'. */
    while(p[0] == '/')
        ++p;

    for(first = 1, last = 0; !last ; ++p)
    {
        if(p[0] == '\0')
            last = 1;
        else if (p[0] != '/')
            continue;
        *p = '\0';
        if(!last && p[1] == '\0')
            last = 1;

        /* Modified by the umask */
        if(mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0)
        {
            if(errno == EEXIST || errno == EISDIR)
            {
                if(stat(path, &sb) < 0)
                {
                    retval = 1;
                    break;
                }
                else if(!S_ISDIR(sb.st_mode))
                {
                    if (last)
                        errno = EEXIST;
                    else
                        errno = ENOTDIR;
                    retval = 1;
                    break;
                }
            }
            else
            {
                retval = 1;
                break;
            }
        }
        if (!last)
            *p = '/';
    }

    return (retval);
}

static int
create_dir_for_file(const char* path)
{
    char *p = strrchr(path, '/');
    char *dir;
    int r;

    /* No subdirectories, not needed */
    if (!p)
        return 0;

    dir = calloc((p - path) + 1, 1);
    if(!dir)
    {
        errno = ENOMEM;
        return -1;
    }

    memcpy (dir, path, (p - path));
    r = mkdir_p(dir);
    free(dir);

    if(r < 0)
    {
        warnx("couldn't create directory for rrd file: %s", dir);
        return r;
    }

    return r;
}

static int
create_file(create_ctx* ctx, const char* rrd)
{
    create_arg* args = NULL;
    create_arg* arg;
    rra_arg* rra;
    field_arg* field;
    int nargs = 0;
    uint rows, steps;
    int argc, r;
    const char** argv;

    if(!ctx->interval)
    {
        warnx("%s: missing interval option", ctx->confname);
        return -1;
    }

    if(!ctx->fields)
    {
        warnx("%s: no fields defined", ctx->confname);
        return -1;
    }


    /* Build all the RRAs */
    for(rra = ctx->rras; rra; rra = rra->next)
    {
        ASSERT(rra->per);
        ASSERT(rra->num);
        ASSERT(rra->total);

        steps = (rra->per / rra->num) / ctx->interval;
        if(!steps)
        {
            warnx("%s: archive has too many data points for polling interval. ignoring",
                  ctx->confname);
            continue;
        }
        rows = rra->total / (ctx->interval * steps);

        arg = (create_arg*)xcalloc(sizeof(create_arg));
        snprintf(arg->buf, sizeof(arg->buf), "RRA:%s:0.6:%d:%d",
                 ctx->cf, steps, rows);
        arg->buf[sizeof(arg->buf) - 1] = 0;
        arg->next = args;
        args = arg;
        nargs++;
    }

    if(!nargs)
    {
        warnx("%s: no archives defined", ctx->confname);
        return -1;
    }


    /* Build all the fields */
    for(field = ctx->fields; field; field = field->next)
    {
        ASSERT(field->name);
        arg = (create_arg*)xcalloc(sizeof(create_arg));
        snprintf(arg->buf, sizeof(arg->buf), "DS:%s:%s:%d:%s:%s",
                 field->name, field->dst, ctx->interval * 3, field->min, field->max);
        arg->buf[sizeof(arg->buf) - 1] = 0;
        arg->next = args;
        args = arg;
        nargs++;
    }

    /* And the interval */
    arg = (create_arg*)xcalloc(sizeof(create_arg));
    snprintf(arg->buf, sizeof(arg->buf), "-s%d", ctx->interval);
    arg->buf[sizeof(arg->buf) - 1] = 0;
    arg->next = args;
    args = arg;
    nargs++;

    argv = (const char**)xcalloc(sizeof(char*) * (nargs + 4));

    if(create_dir_for_file(rrd) >= 0)
    {
        argv[0] = "create";
        argv[1] = rrd;
        argv[2] = "-b-1y";  /* Allow stuff up to a year old */
        argc = 3;

        if(!g_print)
            verb("creating rrd with command:");

        if(g_verbose || g_print)
            fprintf(stderr, "# rrdtool create '%s' -b-1y ", rrd);

        for(arg = args; arg; arg = arg->next)
        {
            argv[argc++] = arg->buf;
            if(g_verbose || g_print)
                fprintf(stderr, "%s ", arg->buf);
        }

        if(g_verbose || g_print)
            fprintf(stderr, "\n");

        if(!g_print)
        {
            /* Always have to clear before calling rrdtool. klunky :( */
            optind = 0;
            opterr = 0;

            rrd_clear_error();
            r = rrd_create(argc, (char**)argv);

            if(r != 0)
                warnx("couldn't create rrd file: %s: %s", rrd, rrd_get_error());
            else if(!g_print)
                verb("created rrd: %s", rrd);
        }
    }

    /* Some cleanup */
    free(argv);
    while(args)
    {
        arg = args->next;
        free(args);
        args = arg;
    }

    /* We've handled all our own errors */
    return 0;
}

static void
check_create_file(create_ctx* ctx)
{
    char rrd[MAXPATHLEN];

    ASSERT(ctx->confname);

    /* No create section, no create */
    if(!ctx->create)
        return;

    if(ctx->rrdname)
        strlcpy(rrd, ctx->rrdname, sizeof(rrd));
    else
    {
        snprintf(rrd, sizeof(rrd), "%s/%s.rrd", ctx->workdir, ctx->confname);
        rrd[sizeof(rrd) - 1] = 0;
    }

    if(!g_print)
    {
        /* Make sure it exists */
        if(access(rrd, F_OK) == 0)
        {
            verb("rrd file already exists, skipping: %s", rrd);
            return;
        }
        else if(errno != ENOENT)
        {
            warn("couldn't check rrd file: %s", rrd);
            return;
        }
    }

    if(ctx->skip || create_file(ctx, rrd) < 0)
        warnx("skipping rrd creation due to configuration errors: %s", rrd);
}

static uint
unit_to_seconds(char* unit)
{
    strlwr(unit);
    if(strcmp(unit, VAL_SECOND) == 0 ||
            strcmp(unit, VAL_SECONDS) == 0 ||
            strcmp(unit, VAL_SECONDLY) == 0)
        return 1;
    else if(strcmp(unit, VAL_MINUTE) == 0 ||
            strcmp(unit, VAL_MINUTES) == 0 ||
            strcmp(unit, VAL_MINUTELY) == 0)
        return 60;
    else if(strcmp(unit, VAL_HOUR) == 0 ||
            strcmp(unit, VAL_HOURS) == 0 ||
            strcmp(unit, VAL_HOURLY) == 0)
        return 3600;
    else if(strcmp(unit, VAL_DAY) == 0 ||
            strcmp(unit, VAL_DAYS) == 0 ||
            strcmp(unit, VAL_DAILY) == 0)
        return 86400;
    else if(strcmp(unit, VAL_WEEK) == 0 ||
            strcmp(unit, VAL_WEEKS) == 0 ||
            strcmp(unit, VAL_WEEKLY) == 0)
        return 604800;
    else if(strcmp(unit, VAL_MONTH) == 0 ||
            strcmp(unit, VAL_MONTHS) == 0 ||
            strcmp(unit, VAL_MONTHLY) == 0)
        return 2592000;
    else if(strcmp(unit, VAL_YEAR) == 0 ||
            strcmp(unit, VAL_YEARS) == 0 ||
            strcmp(unit, VAL_YEARLY) == 0)
        return 31536000;
    else
        return 0;
}

static int
add_rras(create_ctx* ctx, char* value)
{
    uint per;
    uint num;
    uint many;
    uint unit;
    rra_arg* rra;
    char* t;
    char* p;
    char* p2;

    /*
     * Looks like:
     *     10/minute, 10/hour, 10/day, 10/week * 2 weeks, 1/month * 1year, 5/year
     */

    while(value && *value)
    {
        per = num = 0;
        many = 1;

        /* Skip any delimiters, and parse next */
        value = value + strspn(value, " \t,");
        t = strchr(value, ',');
        if(t)
            *(t++) = 0;

        /* Parse out the number */
        p = strchr(value, '/');
        if(!p)
        {
            warnx("%s: invalid 'archive' option: %s", ctx->confname, value);
            return -1;
        }

        *(p++) = 0;
        num = strtoul(value, &p2, 10);
        if(*p2 || !num)
        {
            warnx("%s: invalid 'archive' factor: %s", ctx->confname, value);
            return -1;
        }


        /* Parse out the time frame */
        p2 = strchr(p, '*');
        if(p2)
            *(p2)++ = 0;

        p = strtrim(p);
        unit = per = unit_to_seconds(p);
        if(per <= 0)
        {
            warnx("%s: invalid 'archive' time unit: %s", ctx->confname, p);
            return -1;
        }

        /* Parse out how many */
        if(p2)
        {
            strtrim(p2);
            many = strtoul(p2, &p, 10);
            if(many <= 0)
            {
                warnx("%s: invalid 'archive' count: %s", ctx->confname, p2);
                return -1;
            }

            /* Is there a unit in the many? */
            p = strtrim(p);
            if(*p)
            {
                unit = unit_to_seconds(p);
                if(unit <= 0)
                {
                    warnx("%s: invalid 'archive' time unit: %s", ctx->confname, p);
                    return -1;
                }
            }
        }

        rra = (rra_arg*)xcalloc(sizeof(rra_arg));
        rra->num = num;
        rra->per = per;
        rra->total = many * unit;
        rra->next = ctx->rras;
        ctx->rras = rra;
        value = t;
    }

    return 0;
}


/* -----------------------------------------------------------------------------
 * CONFIG CALLBACKS
 */

int
cfg_value(const char* filename, const char* header, const char* name,
          char* value, void* data)
{
    create_ctx* ctx = (create_ctx*)data;
    char* suffix;
    char* t;

    ASSERT(filename);
    ASSERT(ctx);

    if(!ctx->confname)
        ctx->confname = filename;

    /* Called like this after each config file */
    if(!header)
    {
        /* Create this file (if necessary) */
        check_create_file(ctx);

        /* Do cleanup */
        context_reset(ctx);

        return 0;
    }

    ASSERT(name && value);

    /* [poll] section */
    if(strcmp(header, CONFIG_POLL) == 0)
    {
        /* Interval option */
        if(strcmp(name, CONFIG_INTERVAL) == 0)
        {
            ctx->interval = strtoul(value, &t, 10);
            if(*t || !ctx->interval)
            {
                warnx("%s: invalid 'interval' value: %s", ctx->confname, value);
                ctx->skip = 1;
            }
        }

        /* Ignore other options */
        return 0;
    }

    if(strcmp(header, CONFIG_GENERAL) == 0)
    {
        /* rrd option */
        if(strcmp(name, CONFIG_RRD) == 0)
            ctx->rrdname = value;

        /* Ignore other options */
        return 0;
    }

    /* The rest is in the [create] section */
    if(strcmp(header, CONFIG_CREATE) != 0)
        return 0;

    /* Have a [create] section */
    ctx->create = 1;

    /* The cf option */
    if(strcmp(name, CONFIG_CF) == 0)
    {
        strupr(value);
        if(strcmp(value, VAL_AVERAGE) == 0 ||
           strcmp(value, VAL_MIN) == 0 ||
           strcmp(value, VAL_MAX) == 0 ||
           strcmp(value, VAL_LAST) == 0)
        {
            ctx->cf = value;
        }
        else
        {
            warnx("%s: invalid 'cf' value: %s", ctx->confname, value);
            ctx->skip = 1;
        }

        ctx->create = 0;
        return 0;
    }

    /* The archive option */
    if(strcmp(name, CONFIG_ARCHIVE) == 0)
    {
        if(add_rras(ctx, value) < 0)
            ctx->skip = 1;
        return 0;
    }

    /* Try and see if the field has a suffix */
    suffix = strchr(name, '.');
    if(!suffix) /* Ignore unknown options */
        return 0;

    /* Have a [create] section */
    ctx->create = 1;

    *suffix = 0;
    suffix++;

    /* Make sure the field name is good */
    t = (char*)name + strspn(name, FIELD_VALID);
    if(*t)
    {
        warnx("%s: the '%s' field name must only contain characters, digits, underscore and dash",
              ctx->confname, name);
        ctx->skip = 1;
        return 0;
    }

    /* Field type suffix */
    if(strcmp(suffix, CONFIG_TYPE) == 0)
    {
        strupr(value);
        if(strcmp(value, VAL_ABSOLUTE) == 0 ||
           strcmp(value, VAL_COUNTER) == 0 ||
           strcmp(value, VAL_GAUGE) == 0 ||
           strcmp(value, VAL_DERIVE) == 0)
        {
            field_for(ctx, (char*)name)->dst = value;
        }
        else
        {
            warnx("%s: invalid field type: %s", ctx->confname, value);
            ctx->skip = 1;
        }

        return 0;
    }

    /* Field minimum */
    if(strcmp(suffix, CONFIG_MIN) == 0)
    {
        strupr(value);
        if(strcmp(value, VAL_UNKNOWN) != 0)
        {
            strtod(value, &t);
            if(*t)
            {
                warnx("%s: invalid field min: %s", ctx->confname, value);
                ctx->skip = 1;
                return 0;
            }
        }

        field_for(ctx, (char*)name)->min = value;
        return 0;
    }

    /* Field maximum */
    if(strcmp(suffix, CONFIG_MAX) == 0)
    {
        strupr(value);
        if(strcmp(value, VAL_UNKNOWN) != 0)
        {
            strtod(value, &t);
            if(*t)
            {
                warnx("%s: invalid field max: %s", ctx->confname, value);
                ctx->skip = 1;
                return 0;
            }
        }

        field_for(ctx, (char*)name)->max = value;
        return 0;
    }

    /* Ignore unknown options */
    return 0;
}

int
cfg_error(const char* filename, const char* errmsg, void* data)
{
    create_ctx* ctx = (create_ctx*)data;

    /* Skip the file on errors */
    ctx->skip = 1;

    warnx("%s", errmsg);
    return 0;
}


/* -----------------------------------------------------------------------------
 * STARTUP
 */

static void
usage()
{
    fprintf(stderr, "usage: rrdbot-create [-vn] [-c confdir] [-w workdir]\n");
    fprintf(stderr, "       rrdbot-create -V\n");
    exit(2);
}

static void
version()
{
    printf("rrdbot-create (version %s)\n", VERSION);
    printf("   default config directory: %s\n", DEFAULT_CONFIG);
    printf("   default work directory:   %s\n", DEFAULT_WORK);
    exit(0);
}

int
main(int argc, char* argv[])
{
    const char* confdir = DEFAULT_CONFIG;
    create_ctx ctx;
    char ch;

    memset(&ctx, 0, sizeof(ctx));
    ctx.workdir = DEFAULT_WORK;

    /* Parse the arguments nicely */
    while((ch = getopt(argc, argv, "c:nw:vV")) != -1)
    {
        switch(ch)
        {

        /* Config directory */
        case 'c':
            confdir = optarg;
            break;

        /* Only print commands */
        case 'n':
            g_print = 1;
            break;

        /* Be verbose */
        case 'v':
            g_verbose = 1;
            break;

        /* Print version number */
        case 'V':
            version();
            break;

        /* The work directory */
        case 'w':
            ctx.workdir = optarg;
            break;

        /* Usage information */
        case '?':
        default:
            usage();
            break;
        }
    }

    argc -= optind;
    argv += optind;

    if(argc != 0)
        usage();

    context_reset(&ctx);

    /*
     * We parse the configuration, this calls cfg_value
     * which will do the actual creation of the files
     */
    if(cfg_parse_dir(confdir, &ctx) == -1)
        exit(1); /* message already printed */

    return 0;
}