@@ -194,6 +194,8 @@ static void help(const char *prog)
194
194
195
195
printf (" -f <format> - Log format. See below for details.\n" );
196
196
printf (" - Use -f \"<format>\" so your shell doesn't break it up.\n" );
197
+ printf (" -N - Prefix \"%%UPSHOST%%%%t\" before the format (default/custom)" );
198
+ printf (" - Useful when logging many systems into same target.\n" );
197
199
printf (" -i <interval> - Time between updates, in seconds\n" );
198
200
printf (" -d <count> - Exit after specified amount of updates\n" );
199
201
printf (" -l <logfile> - Log file name, or - for stdout (foreground by default)\n" );
@@ -493,7 +495,7 @@ static void run_flist(const struct monhost_ups_t *monhost_ups_print)
493
495
494
496
int main (int argc , char * * argv )
495
497
{
496
- int interval = 30 , i , foreground = -1 ;
498
+ int interval = 30 , i , foreground = -1 , prefix_UPSHOST = 0 , logformat_allocated = 0 ;
497
499
size_t monhost_len = 0 , loop_count = 0 ;
498
500
const char * prog = xbasename (argv [0 ]);
499
501
time_t now , nextpoll = 0 ;
@@ -508,7 +510,7 @@ int main(int argc, char **argv)
508
510
509
511
print_banner_once (prog , 0 );
510
512
511
- while ((i = getopt (argc , argv , "+hDs:l:i:d:f :u:Vp:FBm:" )) != -1 ) {
513
+ while ((i = getopt (argc , argv , "+hDs:l:i:d:Nf :u:Vp:FBm:" )) != -1 ) {
512
514
switch (i ) {
513
515
case 'h' :
514
516
help (prog );
@@ -598,6 +600,10 @@ int main(int argc, char **argv)
598
600
logformat = optarg ;
599
601
break ;
600
602
603
+ case 'N' :
604
+ prefix_UPSHOST = 1 ;
605
+ break ;
606
+
601
607
case 'u' :
602
608
user = optarg ;
603
609
break ;
@@ -655,6 +661,7 @@ int main(int argc, char **argv)
655
661
656
662
logformat = xmalloc (LARGEBUF );
657
663
memset (logformat , '\0' , LARGEBUF );
664
+ logformat_allocated = 1 ;
658
665
659
666
for (i = 3 ; i < argc ; i ++ )
660
667
snprintfcat (logformat , LARGEBUF , "%s " , argv [i ]);
@@ -694,6 +701,23 @@ int main(int argc, char **argv)
694
701
if (!logformat )
695
702
fatalx (EXIT_FAILURE , "No format defined - but this should be impossible" );
696
703
704
+ if (prefix_UPSHOST ) {
705
+ char * s = xstrdup (logformat );
706
+ if (s ) {
707
+ if (!logformat_allocated ) {
708
+ logformat = xmalloc (LARGEBUF );
709
+ if (!logformat )
710
+ fatalx (EXIT_FAILURE , "Failed re-allocation to prepend UPSHOST to formatting string" );
711
+ memset (logformat , '\0' , LARGEBUF );
712
+ }
713
+ snprintf (logformat , LARGEBUF , "%%UPSHOST%%%%t%s" , s );
714
+ free (s );
715
+ } else {
716
+ upslogx (LOG_WARNING , "Failed to prepend UPSHOST to formatting string" );
717
+ }
718
+ }
719
+ upsdebugx (1 , "logformat: %s" , logformat );
720
+
697
721
/* shouldn't happen */
698
722
if (!monhost_len )
699
723
fatalx (EXIT_FAILURE , "No UPS defined for monitoring - use -s <system> -l <logfile>, or use -m <ups,logfile>" );
@@ -855,7 +879,7 @@ int main(int argc, char **argv)
855
879
if (monhost_ups_current -> logtarget -> logfile != stdout )
856
880
upslogx (LOG_INFO , "NOTE: File %s is already receiving other logs" ,
857
881
monhost_ups_current -> logtarget -> logfn );
858
- upslogx (LOG_INFO , "NOTE: Consider adding %%UPSHOST%% to the log formatting string" );
882
+ upslogx (LOG_INFO , "NOTE: Consider adding %%UPSHOST%% to the log formatting string, e.g. pass -N on CLI " );
859
883
}
860
884
} else {
861
885
if (strcmp (monhost_ups_current -> logtarget -> logfn , "-" ) == 0 )
@@ -967,6 +991,11 @@ int main(int argc, char **argv)
967
991
upscli_disconnect (monhost_ups_current -> ups );
968
992
}
969
993
994
+ if (logformat_allocated ) {
995
+ free (logformat );
996
+ logformat = NULL ;
997
+ }
998
+
970
999
exit (EXIT_SUCCESS );
971
1000
}
972
1001
0 commit comments