@@ -287,10 +287,31 @@ static int rc_config (oconfig_item_t *ci)
287
287
return (0 );
288
288
} /* int rc_config */
289
289
290
+ static int try_reconnect (void )
291
+ {
292
+ int status ;
293
+
294
+ rrdc_disconnect ();
295
+
296
+ rrd_clear_error ();
297
+ status = rrdc_connect (daemon_address );
298
+ if (status != 0 )
299
+ {
300
+ ERROR ("rrdcached plugin: Failed to reconnect to RRDCacheD "
301
+ "at %s: %s (status=%d)" , daemon_address , rrd_get_error (), status );
302
+ return (-1 );
303
+ }
304
+
305
+ INFO ("rrdcached plugin: Successfully reconnected to RRDCacheD "
306
+ "at %s" , daemon_address );
307
+ return (0 );
308
+ } /* int try_reconnect */
309
+
290
310
static int rc_read (void )
291
311
{
292
312
int status ;
293
313
rrdc_stats_t * head ;
314
+ _Bool retried = 0 ;
294
315
295
316
value_t values [1 ];
296
317
value_list_t vl = VALUE_LIST_INIT ;
@@ -311,19 +332,35 @@ static int rc_read (void)
311
332
sstrncpy (vl .host , daemon_address , sizeof (vl .host ));
312
333
sstrncpy (vl .plugin , "rrdcached" , sizeof (vl .plugin ));
313
334
335
+ rrd_clear_error ();
314
336
status = rrdc_connect (daemon_address );
315
337
if (status != 0 )
316
338
{
317
- ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i." ,
318
- daemon_address , status );
339
+ ERROR ("rrdcached plugin: Failed to connect to RRDCacheD "
340
+ "at %s: %s (status=%d)" , daemon_address , rrd_get_error () , status );
319
341
return (-1 );
320
342
}
321
343
322
- head = NULL ;
323
- status = rrdc_stats_get (& head );
324
- if (status != 0 )
344
+ while (42 )
325
345
{
326
- ERROR ("rrdcached plugin: rrdc_stats_get failed with status %i." , status );
346
+ /* The RRD client lib does not provide any means for checking a
347
+ * connection, hence we'll have to retry upon failed operations. */
348
+ head = NULL ;
349
+ rrd_clear_error ();
350
+ status = rrdc_stats_get (& head );
351
+ if (status == 0 )
352
+ break ;
353
+
354
+ if (!retried )
355
+ {
356
+ retried = 1 ;
357
+ if (try_reconnect () == 0 )
358
+ continue ;
359
+ /* else: report the error and fail */
360
+ }
361
+
362
+ ERROR ("rrdcached plugin: rrdc_stats_get failed: %s (status=%i)." ,
363
+ rrd_get_error (), status );
327
364
return (-1 );
328
365
}
329
366
@@ -411,6 +448,7 @@ static int rc_write (const data_set_t *ds, const value_list_t *vl,
411
448
char values [512 ];
412
449
char * values_array [2 ];
413
450
int status ;
451
+ _Bool retried = 0 ;
414
452
415
453
if (daemon_address == NULL )
416
454
{
@@ -467,20 +505,34 @@ static int rc_write (const data_set_t *ds, const value_list_t *vl,
467
505
}
468
506
}
469
507
508
+ rrd_clear_error ();
470
509
status = rrdc_connect (daemon_address );
471
510
if (status != 0 )
472
511
{
473
- ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i." ,
474
- daemon_address , status );
512
+ ERROR ("rrdcached plugin: Failed to connect to RRDCacheD "
513
+ "at %s: %s (status=%d)" , daemon_address , rrd_get_error () , status );
475
514
return (-1 );
476
515
}
477
516
478
- status = rrdc_update (filename , /* values_num = */ 1 , (void * ) values_array );
479
- if (status != 0 )
517
+ while (42 )
480
518
{
481
- ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
482
- "status %i." ,
483
- filename , values_array [0 ], status );
519
+ /* The RRD client lib does not provide any means for checking a
520
+ * connection, hence we'll have to retry upon failed operations. */
521
+ rrd_clear_error ();
522
+ status = rrdc_update (filename , /* values_num = */ 1 , (void * ) values_array );
523
+ if (status == 0 )
524
+ break ;
525
+
526
+ if (!retried )
527
+ {
528
+ retried = 1 ;
529
+ if (try_reconnect () == 0 )
530
+ continue ;
531
+ /* else: report the error and fail */
532
+ }
533
+
534
+ ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed: %s (status=%i)" ,
535
+ filename , values_array [0 ], rrd_get_error (), status );
484
536
return (-1 );
485
537
}
486
538
@@ -493,6 +545,7 @@ static int rc_flush (__attribute__((unused)) cdtime_t timeout, /* {{{ */
493
545
{
494
546
char filename [PATH_MAX + 1 ];
495
547
int status ;
548
+ _Bool retried = 0 ;
496
549
497
550
if (identifier == NULL )
498
551
return (EINVAL );
@@ -502,19 +555,34 @@ static int rc_flush (__attribute__((unused)) cdtime_t timeout, /* {{{ */
502
555
else
503
556
ssnprintf (filename , sizeof (filename ), "%s.rrd" , identifier );
504
557
558
+ rrd_clear_error ();
505
559
status = rrdc_connect (daemon_address );
506
560
if (status != 0 )
507
561
{
508
- ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i." ,
509
- daemon_address , status );
562
+ ERROR ("rrdcached plugin: Failed to connect to RRDCacheD "
563
+ "at %s: %s (status=%d)" , daemon_address , rrd_get_error () , status );
510
564
return (-1 );
511
565
}
512
566
513
- status = rrdc_flush (filename );
514
- if (status != 0 )
567
+ while (42 )
515
568
{
516
- ERROR ("rrdcached plugin: rrdc_flush (%s) failed with status %i." ,
517
- filename , status );
569
+ /* The RRD client lib does not provide any means for checking a
570
+ * connection, hence we'll have to retry upon failed operations. */
571
+ rrd_clear_error ();
572
+ status = rrdc_flush (filename );
573
+ if (status == 0 )
574
+ break ;
575
+
576
+ if (!retried )
577
+ {
578
+ retried = 1 ;
579
+ if (try_reconnect () == 0 )
580
+ continue ;
581
+ /* else: report the error and fail */
582
+ }
583
+
584
+ ERROR ("rrdcached plugin: rrdc_flush (%s) failed: %s (status=%i)." ,
585
+ filename , rrd_get_error (), status );
518
586
return (-1 );
519
587
}
520
588
DEBUG ("rrdcached plugin: rrdc_flush (%s): Success." , filename );
0 commit comments