forked from open-falcon/rrdlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rrd_update.c
1707 lines (1524 loc) · 50.3 KB
/
rrd_update.c
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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* RRDtool 1.4.9 Copyright by Tobi Oetiker, 1997-2014
* Copyright by Florian Forster, 2008
*****************************************************************************
* rrd_update.c RRD Update Function
*****************************************************************************
* $Id$
*****************************************************************************/
#include "rrd_tool.h"
#define DISABLE_USEC
#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
#include <sys/locking.h>
#include <sys/stat.h>
#include <io.h>
#endif
#include <locale.h>
#include "rrd_hw.h"
#include "rrd_rpncalc.h"
#include "rrd_is_thread_safe.h"
#include "unused.h"
#ifndef RRD_LITE
#include "rrd_client.h"
#endif
#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
/*
* WIN32 does not have gettimeofday and struct timeval. This is a quick and dirty
* replacement.
*/
#include <sys/timeb.h>
#ifndef __MINGW32__
struct timeval {
time_t tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
#endif
struct __timezone {
int tz_minuteswest; /* minutes W of Greenwich */
int tz_dsttime; /* type of dst correction */
};
static int gettimeofday( struct timeval *t, struct __timezone *tz) {
struct _timeb current_time;
_ftime(¤t_time);
t->tv_sec = current_time.time;
t->tv_usec = current_time.millitm * 1000;
return 0;
}
#endif
/* FUNCTION PROTOTYPES */
int rrd_update_r( const char *filename, const char *tmplt,
int argc, const char **argv);
int _rrd_update( const char *filename, const char *tmplt,
int argc, const char **argv, rrd_info_t *);
static int allocate_data_structures( rrd_t *rrd, char ***updvals,
rrd_value_t **pdp_temp, const char *tmplt, long **tmpl_idx,
unsigned long *tmpl_cnt, unsigned long **rra_step_cnt,
unsigned long **skip_update, rrd_value_t **pdp_new);
static int parse_template( rrd_t *rrd, const char *tmplt,
unsigned long *tmpl_cnt, long *tmpl_idx);
static int process_arg( char *step_start, rrd_t *rrd, rrd_file_t *rrd_file,
unsigned long rra_begin, time_t *current_time,
unsigned long *current_time_usec, rrd_value_t *pdp_temp,
rrd_value_t *pdp_new, unsigned long *rra_step_cnt,
char **updvals, long *tmpl_idx, unsigned long tmpl_cnt,
rrd_info_t ** pcdp_summary, int version,
unsigned long *skip_update, int *schedule_smooth);
static int parse_ds( rrd_t *rrd, char **updvals, long *tmpl_idx,
char *input, unsigned long tmpl_cnt, time_t *current_time,
unsigned long *current_time_usec, int version);
static int get_time_from_reading( rrd_t *rrd, char timesyntax,
char **updvals, time_t *current_time,
unsigned long *current_time_usec, int version);
static int update_pdp_prep( rrd_t *rrd, char **updvals,
rrd_value_t *pdp_new, double interval);
static int calculate_elapsed_steps( rrd_t *rrd, unsigned long current_time,
unsigned long current_time_usec, double interval,
double *pre_int, double *post_int, unsigned long *proc_pdp_cnt);
static void simple_update( rrd_t *rrd, double interval, rrd_value_t *pdp_new);
static int process_all_pdp_st( rrd_t *rrd, double interval, double pre_int,
double post_int, unsigned long elapsed_pdp_st, rrd_value_t *pdp_new,
rrd_value_t *pdp_temp);
static int process_pdp_st( rrd_t *rrd, unsigned long ds_idx, double interval,
double pre_int, double post_int, long diff_pdp_st, rrd_value_t *pdp_new,
rrd_value_t *pdp_temp);
static int update_all_cdp_prep( rrd_t *rrd, unsigned long *rra_step_cnt,
unsigned long rra_begin, rrd_file_t *rrd_file,
unsigned long elapsed_pdp_st, unsigned long proc_pdp_cnt,
rrd_value_t **last_seasonal_coef, rrd_value_t **seasonal_coef,
rrd_value_t *pdp_temp, unsigned long *skip_update,
int *schedule_smooth);
static int do_schedule_smooth( rrd_t *rrd, unsigned long rra_idx,
unsigned long elapsed_pdp_st);
static int update_cdp_prep( rrd_t *rrd, unsigned long elapsed_pdp_st,
unsigned long start_pdp_offset, unsigned long *rra_step_cnt, int rra_idx,
rrd_value_t *pdp_temp, rrd_value_t *last_seasonal_coef,
rrd_value_t *seasonal_coef, int current_cf);
static void update_cdp( unival *scratch, int current_cf,
rrd_value_t pdp_temp_val, unsigned long rra_step_cnt,
unsigned long elapsed_pdp_st, unsigned long start_pdp_offset,
unsigned long pdp_cnt, rrd_value_t xff, int i, int ii);
static void initialize_cdp_val( unival *scratch, int current_cf,
rrd_value_t pdp_temp_val, unsigned long start_pdp_offset,
unsigned long pdp_cnt);
static int reset_cdp( rrd_t *rrd, unsigned long elapsed_pdp_st,
rrd_value_t *pdp_temp, rrd_value_t *last_seasonal_coef,
rrd_value_t *seasonal_coef, int rra_idx,
int ds_idx, int cdp_idx, enum cf_en current_cf);
static rrd_value_t initialize_carry_over( rrd_value_t pdp_temp_val,
int current_cf, unsigned long elapsed_pdp_st,
unsigned long start_pdp_offset, unsigned long pdp_cnt);
static rrd_value_t calculate_cdp_val( rrd_value_t cdp_val,
rrd_value_t pdp_temp_val, unsigned long elapsed_pdp_st,
int current_cf, int i, int ii);
static int update_aberrant_cdps(
rrd_t *rrd,
rrd_file_t *rrd_file,
unsigned long rra_begin,
unsigned long elapsed_pdp_st,
rrd_value_t *pdp_temp,
rrd_value_t **seasonal_coef);
static int write_to_rras(
rrd_t *rrd,
rrd_file_t *rrd_file,
unsigned long *rra_step_cnt,
unsigned long rra_begin,
time_t current_time,
unsigned long *skip_update,
rrd_info_t ** pcdp_summary);
static int write_RRA_row(
rrd_file_t *rrd_file,
rrd_t *rrd,
unsigned long rra_idx,
unsigned short CDP_scratch_idx,
rrd_info_t ** pcdp_summary,
time_t rra_time);
static int smooth_all_rras(
rrd_t *rrd,
rrd_file_t *rrd_file,
unsigned long rra_begin);
#ifndef HAVE_MMAP
static int write_changes_to_disk(
rrd_t *rrd,
rrd_file_t *rrd_file,
int version);
#endif
/*
* normalize time as returned by gettimeofday. usec part must
* be always >= 0
*/
static void normalize_time( struct timeval *t) {
if (t->tv_usec < 0) {
t->tv_sec--;
t->tv_usec += 1e6L;
}
}
/*
* Sets current_time and current_time_usec based on the current time.
* current_time_usec is set to 0 if the version number is 1 or 2.
*/
static void initialize_time( time_t *current_time, unsigned long *current_time_usec,
int version) {
struct timeval tmp_time; /* used for time conversion */
gettimeofday(&tmp_time, 0);
normalize_time(&tmp_time);
*current_time = tmp_time.tv_sec;
if (version >= 3) {
*current_time_usec = tmp_time.tv_usec;
} else {
*current_time_usec = 0;
}
#ifdef DISABLE_USEC
*current_time_usec = 0;
#endif
}
#define IFDNAN(X,Y) (isnan(X) ? (Y) : (X));
int rrd_update_r( const char *filename, const char *tmplt, int argc,
const char **argv) {
return _rrd_update(filename, tmplt, argc, argv, NULL);
}
int _rrd_update( const char *filename, const char *tmplt,
int argc, const char **argv, rrd_info_t * pcdp_summary) {
int arg_i = 2;
unsigned long rra_begin; /* byte pointer to the rra
* area in the rrd file. this
* pointer never changes value */
rrd_value_t *pdp_new; /* prepare the incoming data to be added
* to the existing entry */
rrd_value_t *pdp_temp; /* prepare the pdp values to be added
* to the cdp values */
long *tmpl_idx; /* index representing the settings
* transported by the tmplt index */
unsigned long tmpl_cnt = 2; /* time and data */
rrd_t rrd;
time_t current_time = 0;
unsigned long current_time_usec = 0; /* microseconds part of current time */
char **updvals;
int schedule_smooth = 0;
/* number of elapsed PDP steps since last update */
unsigned long *rra_step_cnt = NULL;
int version; /* rrd version */
rrd_file_t *rrd_file;
char *arg_copy; /* for processing the argv */
unsigned long *skip_update; /* RRAs to advance but not write */
int ret = 0;
/* need at least 1 arguments: data. */
if (argc < 1) {
ret = -RRD_ERR_ARG10;
goto err_out;
}
rrd_init(&rrd);
if ((rrd_file = rrd_open(filename, &rrd, RRD_READWRITE, &ret)) == NULL) {
goto err_free;
}
/* We are now at the beginning of the rra's */
rra_begin = rrd_file->header_len;
version = atoi(rrd.stat_head->version);
initialize_time(¤t_time, ¤t_time_usec, version);
/* get exclusive lock to whole file.
* lock gets removed when we close the file.
*/
if (rrd_lock(rrd_file) != 0) {
ret = -RRD_ERR_LOCK;
goto err_close;
}
if ((ret = allocate_data_structures(&rrd, &updvals,
&pdp_temp, tmplt, &tmpl_idx, &tmpl_cnt,
&rra_step_cnt, &skip_update,
&pdp_new)) < 0) {
goto err_close;
}
/* loop through the arguments. */
for (arg_i = 0; arg_i < argc; arg_i++) {
if ((arg_copy = strdup(argv[arg_i])) == NULL) {
ret = -RRD_ERR_FAILED_STRDUP;
break;
}
ret = process_arg(arg_copy, &rrd, rrd_file, rra_begin,
¤t_time, ¤t_time_usec, pdp_temp, pdp_new,
rra_step_cnt, updvals, tmpl_idx, tmpl_cnt,
&pcdp_summary, version, skip_update,
&schedule_smooth);
if (ret == -RRD_ERR_TIME3) {
//nothing to do
//current_time <= last_up
ret = 0;
}else if(ret < 0){
//ret = -RRD_ERR_ARG9;
free(arg_copy);
break;
}
free(arg_copy);
}
free(rra_step_cnt);
/* if we got here and if there is an error and if the file has not been
* written to, then close things up and return. */
if (ret) {
goto err_free_structures;
}
#ifndef HAVE_MMAP
if ((ret = write_changes_to_disk(&rrd, rrd_file, version)) < -1) {
//ret = -RRD_ERR_WRITE7;
goto err_free_structures;
}
#endif
/* calling the smoothing code here guarantees at most one smoothing
* operation per rrd_update call. Unfortunately, it is possible with bulk
* updates, or a long-delayed update for smoothing to occur off-schedule.
* This really isn't critical except during the burn-in cycles. */
if (schedule_smooth) {
ret = smooth_all_rras(&rrd, rrd_file, rra_begin);
}
/* rrd_dontneed(rrd_file,&rrd); */
rrd_free(&rrd);
rrd_close(rrd_file);
free(pdp_new);
free(tmpl_idx);
free(pdp_temp);
free(skip_update);
free(updvals);
return 0;
err_free_structures:
free(pdp_new);
free(tmpl_idx);
free(pdp_temp);
free(skip_update);
free(updvals);
err_close:
rrd_close(rrd_file);
err_free:
rrd_free(&rrd);
err_out:
return ret;
}
/*
* Allocate some important arrays used, and initialize the template.
*
* When it returns, either all of the structures are allocated
* or none of them are.
*
* Returns 0 on success, < 0 on error.
*/
static int allocate_data_structures( rrd_t *rrd, char ***updvals, rrd_value_t **pdp_temp,
const char *tmplt, long **tmpl_idx, unsigned long *tmpl_cnt, unsigned long **rra_step_cnt,
unsigned long **skip_update, rrd_value_t **pdp_new) {
unsigned i, ii;
int ret = 0;
if ((*updvals = (char **) malloc(sizeof(char *)
* (rrd->stat_head->ds_cnt + 1))) == NULL) {
return -RRD_ERR_MALLOC10;
}
if ((*pdp_temp = (rrd_value_t *) malloc(sizeof(rrd_value_t)
* rrd->stat_head->ds_cnt)) ==
NULL) {
ret = -RRD_ERR_MALLOC11;
goto err_free_updvals;
}
if ((*skip_update = (unsigned long *) malloc(sizeof(unsigned long)
*
rrd->stat_head->rra_cnt)) ==
NULL) {
ret = -RRD_ERR_MALLOC12;
goto err_free_pdp_temp;
}
if ((*tmpl_idx = (long *) malloc(sizeof(unsigned long)
* (rrd->stat_head->ds_cnt + 1))) == NULL) {
ret = -RRD_ERR_MALLOC13;
goto err_free_skip_update;
}
if ((*rra_step_cnt = (unsigned long *) malloc(sizeof(unsigned long)
*
(rrd->stat_head->
rra_cnt))) == NULL) {
ret = -RRD_ERR_MALLOC14;
goto err_free_tmpl_idx;
}
/* initialize tmplt redirector */
/* default config example (assume DS 1 is a CDEF DS)
tmpl_idx[0] -> 0; (time)
tmpl_idx[1] -> 1; (DS 0)
tmpl_idx[2] -> 3; (DS 2)
tmpl_idx[3] -> 4; (DS 3) */
(*tmpl_idx)[0] = 0; /* time */
for (i = 1, ii = 1; i <= rrd->stat_head->ds_cnt; i++) {
if (dst_conv(rrd->ds_def[i - 1].dst) != DST_CDEF)
(*tmpl_idx)[ii++] = i;
}
*tmpl_cnt = ii;
if (tmplt != NULL) {
if (parse_template(rrd, tmplt, tmpl_cnt, *tmpl_idx) < 0) {
ret = -RRD_ERR_PARSE;
goto err_free_rra_step_cnt;
}
}
if ((*pdp_new = (rrd_value_t *) malloc(sizeof(rrd_value_t)
* rrd->stat_head->ds_cnt)) == NULL) {
ret = -RRD_ERR_MALLOC15;
goto err_free_rra_step_cnt;
}
return 0;
err_free_rra_step_cnt:
free(*rra_step_cnt);
err_free_tmpl_idx:
free(*tmpl_idx);
err_free_skip_update:
free(*skip_update);
err_free_pdp_temp:
free(*pdp_temp);
err_free_updvals:
free(*updvals);
return ret;
}
/*
* Parses tmplt and puts an ordered list of DS's into tmpl_idx.
*
* Returns 0 on success.
*/
static int parse_template( rrd_t *rrd, const char *tmplt,
unsigned long *tmpl_cnt, long *tmpl_idx) {
char *dsname, *tmplt_copy;
unsigned int tmpl_len, i;
int ret = 0;
*tmpl_cnt = 1; /* the first entry is the time */
/* we should work on a writeable copy here */
if ((tmplt_copy = strdup(tmplt)) == NULL) {
ret = -RRD_ERR_FAILED_STRDUP1;
goto out;
}
dsname = tmplt_copy;
tmpl_len = strlen(tmplt_copy);
for (i = 0; i <= tmpl_len; i++) {
if (tmplt_copy[i] == ':' || tmplt_copy[i] == '\0') {
tmplt_copy[i] = '\0';
if (*tmpl_cnt > rrd->stat_head->ds_cnt) {
ret = -RRD_ERR_MORE_DS;
goto out_free_tmpl_copy;
}
if ((tmpl_idx[(*tmpl_cnt)++] = ds_match(rrd, dsname) + 1) == 0) {
ret = -RRD_ERR_UNKNOWN_DS_NAME1;
goto out_free_tmpl_copy;
}
/* go to the next entry on the tmplt_copy */
if (i < tmpl_len)
dsname = &tmplt_copy[i + 1];
}
}
out_free_tmpl_copy:
free(tmplt_copy);
out:
return ret;
}
/*
* Parse an update string, updates the primary data points (PDPs)
* and consolidated data points (CDPs), and writes changes to the RRAs.
*
* Returns 0 on success, < 0 on error.
*/
static int process_arg( char *step_start, rrd_t *rrd, rrd_file_t *rrd_file,
unsigned long rra_begin, time_t *current_time,
unsigned long *current_time_usec, rrd_value_t *pdp_temp,
rrd_value_t *pdp_new, unsigned long *rra_step_cnt,
char **updvals, long *tmpl_idx, unsigned long tmpl_cnt,
rrd_info_t ** pcdp_summary, int version, unsigned long *skip_update,
int *schedule_smooth) {
rrd_value_t *seasonal_coef = NULL, *last_seasonal_coef = NULL;
/* a vector of future Holt-Winters seasonal coefs */
unsigned long elapsed_pdp_st;
double interval, pre_int, post_int; /* interval between this and
* the last run */
unsigned long proc_pdp_cnt;
int ret = 0;
ret = parse_ds(rrd, updvals, tmpl_idx, step_start, tmpl_cnt,
current_time, current_time_usec, version);
if (ret) {
return ret;
}
interval = (double) (*current_time - rrd->live_head->last_up)
+ (double) ((long) *current_time_usec -
(long) rrd->live_head->last_up_usec) / 1e6f;
/* process the data sources and update the pdp_prep
* area accordingly */
if ((ret = update_pdp_prep(rrd, updvals, pdp_new, interval)) < 0) {
return ret;
}
elapsed_pdp_st = calculate_elapsed_steps(rrd,
*current_time,
*current_time_usec, interval,
&pre_int, &post_int,
&proc_pdp_cnt);
/* has a pdp_st moment occurred since the last run ? */
if (elapsed_pdp_st == 0) {
/* no we have not passed a pdp_st moment. therefore update is simple */
simple_update(rrd, interval, pdp_new);
} else {
/* an pdp_st has occurred. */
if ((ret = process_all_pdp_st(rrd, interval,
pre_int, post_int,
elapsed_pdp_st, pdp_new, pdp_temp)) < 0) {
return ret;
}
if ((ret = update_all_cdp_prep(rrd, rra_step_cnt,
rra_begin, rrd_file,
elapsed_pdp_st,
proc_pdp_cnt,
&last_seasonal_coef,
&seasonal_coef,
pdp_temp,
skip_update, schedule_smooth)) < 0) {
goto err_free_coefficients;
}
if ((ret = update_aberrant_cdps(rrd, rrd_file, rra_begin,
elapsed_pdp_st, pdp_temp,
&seasonal_coef)) < 0) {
goto err_free_coefficients;
}
if ((ret = write_to_rras(rrd, rrd_file, rra_step_cnt, rra_begin,
*current_time, skip_update,
pcdp_summary)) < 0) {
goto err_free_coefficients;
}
} /* endif a pdp_st has occurred */
rrd->live_head->last_up = *current_time;
rrd->live_head->last_up_usec = *current_time_usec;
if (version < 3) {
*rrd->legacy_last_up = rrd->live_head->last_up;
}
free(seasonal_coef);
free(last_seasonal_coef);
return 0;
err_free_coefficients:
free(seasonal_coef);
free(last_seasonal_coef);
return ret;
}
/*
* Parse a DS string (time + colon-separated values), storing the
* results in current_time, current_time_usec, and updvals.
*
* Returns 0 on success, < 0 on error.
*/
static int parse_ds( rrd_t *rrd, char **updvals, long *tmpl_idx, char *input,
unsigned long tmpl_cnt, time_t *current_time,
unsigned long *current_time_usec, int version) {
char *p;
unsigned long i;
char timesyntax;
int ret = 0;
updvals[0] = input;
/* initialize all ds input to unknown except the first one
which has always got to be set */
for (i = 1; i <= rrd->stat_head->ds_cnt; i++)
updvals[i] = "U";
/* separate all ds elements; first must be examined separately
due to alternate time syntax */
if ((p = strchr(input, '@')) != NULL) {
timesyntax = '@';
} else if ((p = strchr(input, ':')) != NULL) {
timesyntax = ':';
} else {
return -RRD_ERR_STR;
}
*p = '\0';
i = 1;
updvals[tmpl_idx[i++]] = p + 1;
while (*(++p)) {
if (*p == ':') {
*p = '\0';
if (i < tmpl_cnt) {
updvals[tmpl_idx[i++]] = p + 1;
} else {
return -RRD_ERR_ARG11;
}
}
}
if (i != tmpl_cnt) {
return -RRD_ERR_EXPECTED;
}
return get_time_from_reading(rrd, timesyntax, updvals,
current_time, current_time_usec,
version);
}
/*
* Parse the time in a DS string, store it in current_time and
* current_time_usec and verify that it's later than the last
* update for this DS.
*
* Returns 0 on success, < 0 on error.
*/
static int get_time_from_reading( rrd_t *rrd, char timesyntax,
char **updvals, time_t *current_time,
unsigned long *current_time_usec, int version) {
double tmp;
char *parsetime_error = NULL;
char *old_locale;
rrd_time_value_t ds_tv;
struct timeval tmp_time; /* used for time conversion */
/* get the time from the reading ... handle N */
if (timesyntax == '@') { /* at-style */
if ((parsetime_error = rrd_parsetime(updvals[0], &ds_tv))) {
return -RRD_ERR_TIME1;
}
if (ds_tv.type == RELATIVE_TO_END_TIME ||
ds_tv.type == RELATIVE_TO_START_TIME) {
return -RRD_ERR_TIME2;
}
*current_time = mktime(&ds_tv.tm) +ds_tv.offset;
*current_time_usec = 0; /* FIXME: how to handle usecs here ? */
} else if (strcmp(updvals[0], "N") == 0) {
gettimeofday(&tmp_time, 0);
normalize_time(&tmp_time);
*current_time = tmp_time.tv_sec;
*current_time_usec = tmp_time.tv_usec;
} else {
old_locale = setlocale(LC_NUMERIC, "C");
errno = 0;
tmp = strtod(updvals[0], 0);
if (errno > 0) {
return -RRD_ERR_STRTOD;
};
setlocale(LC_NUMERIC, old_locale);
if (tmp < 0.0){
gettimeofday(&tmp_time, 0);
tmp = (double)tmp_time.tv_sec + (double)tmp_time.tv_usec * 1e-6f + tmp;
}
*current_time = floor(tmp);
*current_time_usec = (long) ((tmp - (double) *current_time) * 1e6f);
}
/* dont do any correction for old version RRDs */
if (version < 3)
*current_time_usec = 0;
#ifdef DISABLE_USEC
*current_time_usec = 0;
#endif
if (*current_time < rrd->live_head->last_up ||
(*current_time == rrd->live_head->last_up &&
(long) *current_time_usec <= (long) rrd->live_head->last_up_usec)) {
return -RRD_ERR_TIME3;
}
return 0;
}
/*
* Update pdp_new by interpreting the updvals according to the DS type
* (COUNTER, GAUGE, etc.).
*
* Returns 0 on success, < 0 on error.
*/
static int update_pdp_prep( rrd_t *rrd, char **updvals, rrd_value_t *pdp_new,
double interval) {
unsigned long ds_idx;
int ii;
char *endptr; /* used in the conversion */
double rate;
char *old_locale;
enum dst_en dst_idx;
int ret = 0;
for (ds_idx = 0; ds_idx < rrd->stat_head->ds_cnt; ds_idx++) {
dst_idx = dst_conv(rrd->ds_def[ds_idx].dst);
/* make sure we do not build diffs with old last_ds values */
if (rrd->ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt < interval) {
strncpy(rrd->pdp_prep[ds_idx].last_ds, "U", LAST_DS_LEN - 1);
rrd->pdp_prep[ds_idx].last_ds[LAST_DS_LEN - 1] = '\0';
}
/* NOTE: DST_CDEF should never enter this if block, because
* updvals[ds_idx+1][0] is initialized to 'U'; unless the caller
* accidently specified a value for the DST_CDEF. To handle this case,
* an extra check is required. */
if ((updvals[ds_idx + 1][0] != 'U') &&
(dst_idx != DST_CDEF) &&
rrd->ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt >= interval) {
rate = DNAN;
/* pdp_new contains rate * time ... eg the bytes transferred during
* the interval. Doing it this way saves a lot of math operations
*/
switch (dst_idx) {
case DST_COUNTER:
case DST_DERIVE:
/* Check if this is a valid integer. `U' is already handled in
* another branch. */
for (ii = 0; updvals[ds_idx + 1][ii] != 0; ii++) {
if ((ii == 0) && (dst_idx == DST_DERIVE)
&& (updvals[ds_idx + 1][ii] == '-'))
continue;
if ((updvals[ds_idx + 1][ii] < '0')
|| (updvals[ds_idx + 1][ii] > '9')) {
return -RRD_ERR_INT;
}
} /* for (ii = 0; updvals[ds_idx + 1][ii] != 0; ii++) */
if (rrd->pdp_prep[ds_idx].last_ds[0] != 'U') {
pdp_new[ds_idx] =
rrd_diff(updvals[ds_idx + 1],
rrd->pdp_prep[ds_idx].last_ds);
if (dst_idx == DST_COUNTER) {
/* simple overflow catcher. This will fail
* terribly for non 32 or 64 bit counters
* ... are there any others in SNMP land?
*/
if (pdp_new[ds_idx] < (double) 0.0)
pdp_new[ds_idx] += (double) 4294967296.0; /* 2^32 */
if (pdp_new[ds_idx] < (double) 0.0)
pdp_new[ds_idx] += (double) 18446744069414584320.0; /* 2^64-2^32 */
}
rate = pdp_new[ds_idx] / interval;
} else {
pdp_new[ds_idx] = DNAN;
}
break;
case DST_ABSOLUTE:
old_locale = setlocale(LC_NUMERIC, "C");
errno = 0;
pdp_new[ds_idx] = strtod(updvals[ds_idx + 1], &endptr);
if (errno > 0) {
return -RRD_ERR_STRTOD;
};
setlocale(LC_NUMERIC, old_locale);
if (endptr[0] != '\0') {
return -RRD_ERR_DATA;
}
rate = pdp_new[ds_idx] / interval;
break;
case DST_GAUGE:
old_locale = setlocale(LC_NUMERIC, "C");
errno = 0;
pdp_new[ds_idx] =
strtod(updvals[ds_idx + 1], &endptr) * interval;
if (errno) {
return -RRD_ERR_STRTOD;
};
setlocale(LC_NUMERIC, old_locale);
if (endptr[0] != '\0') {
return -RRD_ERR_DATA;
}
rate = pdp_new[ds_idx] / interval;
break;
default:
return -RRD_ERR_UNKNOWN_DS_TYPE;
}
/* break out of this for loop if the error string is set */
if (ret) {
return ret;
}
/* make sure pdp_temp is neither too large or too small
* if any of these occur it becomes unknown ...
* sorry folks ... */
if (!isnan(rate) &&
((!isnan(rrd->ds_def[ds_idx].par[DS_max_val].u_val) &&
rate > rrd->ds_def[ds_idx].par[DS_max_val].u_val) ||
(!isnan(rrd->ds_def[ds_idx].par[DS_min_val].u_val) &&
rate < rrd->ds_def[ds_idx].par[DS_min_val].u_val))) {
pdp_new[ds_idx] = DNAN;
}
} else {
/* no news is news all the same */
pdp_new[ds_idx] = DNAN;
}
/* make a copy of the command line argument for the next run */
#ifdef DEBUG
fprintf(stderr, "prep ds[%lu]\t"
"last_arg '%s'\t"
"this_arg '%s'\t"
"pdp_new %10.2f\n",
ds_idx, rrd->pdp_prep[ds_idx].last_ds, updvals[ds_idx + 1],
pdp_new[ds_idx]);
#endif
strncpy(rrd->pdp_prep[ds_idx].last_ds, updvals[ds_idx + 1],
LAST_DS_LEN - 1);
rrd->pdp_prep[ds_idx].last_ds[LAST_DS_LEN - 1] = '\0';
}
return 0;
}
/*
* How many PDP steps have elapsed since the last update? Returns the answer,
* and stores the time between the last update and the last PDP in pre_time,
* and the time between the last PDP and the current time in post_int.
*/
static int calculate_elapsed_steps( rrd_t *rrd, unsigned long current_time,
unsigned long current_time_usec, double interval,
double *pre_int, double *post_int, unsigned long *proc_pdp_cnt) {
unsigned long proc_pdp_st; /* which pdp_st was the last to be processed */
unsigned long occu_pdp_st; /* when was the pdp_st before the last update
* time */
unsigned long proc_pdp_age; /* how old was the data in the pdp prep area
* when it was last updated */
unsigned long occu_pdp_age; /* how long ago was the last pdp_step time */
/* when was the current pdp started */
proc_pdp_age = rrd->live_head->last_up % rrd->stat_head->pdp_step;
proc_pdp_st = rrd->live_head->last_up - proc_pdp_age;
/* when did the last pdp_st occur */
occu_pdp_age = current_time % rrd->stat_head->pdp_step;
occu_pdp_st = current_time - occu_pdp_age;
if (occu_pdp_st > proc_pdp_st) {
/* OK we passed the pdp_st moment */
*pre_int = (long) occu_pdp_st - rrd->live_head->last_up; /* how much of the input data
* occurred before the latest
* pdp_st moment*/
*pre_int -= ((double) rrd->live_head->last_up_usec) / 1e6f; /* adjust usecs */
*post_int = occu_pdp_age; /* how much after it */
*post_int += ((double) current_time_usec) / 1e6f; /* adjust usecs */
} else {
*pre_int = interval;
*post_int = 0;
}
*proc_pdp_cnt = proc_pdp_st / rrd->stat_head->pdp_step;
#ifdef DEBUG
printf("proc_pdp_age %lu\t"
"proc_pdp_st %lu\t"
"occu_pfp_age %lu\t"
"occu_pdp_st %lu\t"
"int %lf\t"
"pre_int %lf\t"
"post_int %lf\n", proc_pdp_age, proc_pdp_st,
occu_pdp_age, occu_pdp_st, interval, *pre_int, *post_int);
#endif
/* compute the number of elapsed pdp_st moments */
return (occu_pdp_st - proc_pdp_st) / rrd->stat_head->pdp_step;
}
/*
* Increment the PDP values by the values in pdp_new, or else initialize them.
*/
static void simple_update( rrd_t *rrd, double interval, rrd_value_t *pdp_new) {
int i;
for (i = 0; i < (signed) rrd->stat_head->ds_cnt; i++) {
if (isnan(pdp_new[i])) {
/* this is not really accurate if we use subsecond data arrival time
should have thought of it when going subsecond resolution ...
sorry next format change we will have it! */
rrd->pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt +=
floor(interval);
} else {
if (isnan(rrd->pdp_prep[i].scratch[PDP_val].u_val)) {
rrd->pdp_prep[i].scratch[PDP_val].u_val = pdp_new[i];
} else {
rrd->pdp_prep[i].scratch[PDP_val].u_val += pdp_new[i];
}
}
#ifdef DEBUG
fprintf(stderr,
"NO PDP ds[%i]\t"
"value %10.2f\t"
"unkn_sec %5lu\n",
i,
rrd->pdp_prep[i].scratch[PDP_val].u_val,
rrd->pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt);
#endif
}
}
/*
* Call process_pdp_st for each DS.
*
* Returns 0 on success, < 0 on error.
*/
static int process_all_pdp_st( rrd_t *rrd, double interval,
double pre_int, double post_int, unsigned long elapsed_pdp_st,
rrd_value_t *pdp_new, rrd_value_t *pdp_temp) {
unsigned long ds_idx;
int ret = 0;
/* in pdp_prep[].scratch[PDP_val].u_val we have collected
rate*seconds which occurred up to the last run.
pdp_new[] contains rate*seconds from the latest run.
pdp_temp[] will contain the rate for cdp */
for (ds_idx = 0; ds_idx < rrd->stat_head->ds_cnt; ds_idx++) {
if ((ret = process_pdp_st(rrd, ds_idx, interval, pre_int, post_int,
elapsed_pdp_st * rrd->stat_head->pdp_step,
pdp_new, pdp_temp)) < 0 ) {
return ret;
}
#ifdef DEBUG
fprintf(stderr, "PDP UPD ds[%lu]\t"
"elapsed_pdp_st %lu\t"
"pdp_temp %10.2f\t"
"new_prep %10.2f\t"
"new_unkn_sec %5lu\n",
ds_idx,
elapsed_pdp_st,
pdp_temp[ds_idx],
rrd->pdp_prep[ds_idx].scratch[PDP_val].u_val,
rrd->pdp_prep[ds_idx].scratch[PDP_unkn_sec_cnt].u_cnt);
#endif
}
return 0;
}
/*
* Process an update that occurs after one of the PDP moments.
* Increments the PDP value, sets NAN if time greater than the
* heartbeats have elapsed, processes CDEFs.
*
* Returns 0 on success, < 0 on error.
*/
static int process_pdp_st( rrd_t *rrd, unsigned long ds_idx,
double interval, double pre_int, double post_int,
long diff_pdp_st, /* number of seconds in full steps passed since last update */
rrd_value_t *pdp_new, rrd_value_t *pdp_temp) {
int i;
int ret = 0;
/* update pdp_prep to the current pdp_st. */
double pre_unknown = 0.0;
unival *scratch = rrd->pdp_prep[ds_idx].scratch;
unsigned long mrhb = rrd->ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt;
rpnstack_t rpnstack; /* used for COMPUTE DS */
rpnstack_init(&rpnstack);
if (isnan(pdp_new[ds_idx])) {
/* a final bit of unknown to be added before calculation
we use a temporary variable for this so that we
don't have to turn integer lines before using the value */
pre_unknown = pre_int;
} else {
if (isnan(scratch[PDP_val].u_val)) {
scratch[PDP_val].u_val = 0;
}
scratch[PDP_val].u_val += pdp_new[ds_idx] / interval * pre_int;
}
/* if too much of the pdp_prep is unknown we dump it */
/* if the interval is larger thatn mrhb we get NAN */
if ((interval > mrhb) ||
(rrd->stat_head->pdp_step / 2.0 <
(signed) scratch[PDP_unkn_sec_cnt].u_cnt)) {
pdp_temp[ds_idx] = DNAN;