@@ -221,6 +221,10 @@ int sweepincr = 1; /* payload increment in sweep */
221
221
int interval = 1000 ; /* interval between packets, ms */
222
222
int waittime = MAXWAIT ; /* timeout for each packet */
223
223
long nrcvtimeout = 0 ; /* # of packets we got back after waittime */
224
+ int printmaxtime_ms = 0 ;/* threshold for all pings to get returned before reporting them */
225
+ time_t downsince = 0 ; /* time since last successful ping */
226
+ time_t downtime = 0 ; /* total cumulative time of missed pings */
227
+ char * target ; /* host or ip entered on the command line */
224
228
225
229
/* timing */
226
230
int timing ; /* flag to do timing */
@@ -239,6 +243,7 @@ static void finish(void) __dead2;
239
243
static void pinger (void );
240
244
static char * pr_addr (struct in_addr );
241
245
static char * pr_ntime (n_time );
246
+ static char * timebufnow (char * , int , time_t );
242
247
static void pr_icmph (struct icmp * );
243
248
static void pr_iph (struct ip * );
244
249
static void pr_pack (char * , int , struct sockaddr_in * , struct timeval * );
@@ -262,7 +267,7 @@ main(argc, argv)
262
267
struct sigaction si_sa ;
263
268
size_t sz ;
264
269
u_char * datap , packet [IP_MAXPACKET ];
265
- char * ep , * source , * target , * payload ;
270
+ char * ep , * source , * payload ;
266
271
struct hostent * hp ;
267
272
#ifdef IPSEC_POLICY_IPSEC
268
273
char * policy_in , * policy_out ;
@@ -302,7 +307,7 @@ main(argc, argv)
302
307
303
308
outpack = outpackhdr + sizeof (struct ip );
304
309
while ((ch = getopt (argc , argv ,
305
- "Aab:c:DdfG :g:h:I:i:Ll:M:m:nop:QqRrS:s:T:t:vW:z:"
310
+ "Aab:c:DdF:fG :g:h:I:i:Ll:M:m:nop:QqRrS:s:T:t:vW:z:"
306
311
#ifdef IPSEC
307
312
#ifdef IPSEC_POLICY_IPSEC
308
313
"P:"
@@ -344,6 +349,9 @@ main(argc, argv)
344
349
case 'd' :
345
350
options |= F_SO_DEBUG ;
346
351
break ;
352
+ case 'F' :
353
+ printmaxtime_ms = atoi (optarg );
354
+ break ;
347
355
case 'f' :
348
356
if (uid ) {
349
357
errno = EPERM ;
@@ -816,14 +824,16 @@ main(argc, argv)
816
824
if (to -> sin_family == AF_INET ) {
817
825
(void )printf ("PING %s (%s)" , hostname ,
818
826
inet_ntoa (to -> sin_addr ));
827
+ if (printmaxtime_ms )
828
+ (void )printf (" showing only pings > %d ms" , printmaxtime_ms );
819
829
if (source )
820
830
(void )printf (" from %s" , shostname );
821
831
if (sweepmax )
822
832
(void )printf (": (%d ... %d) data bytes\n" ,
823
833
sweepmin , sweepmax );
824
834
else
825
835
(void )printf (": %d data bytes\n" , datalen );
826
-
836
+
827
837
} else {
828
838
if (sweepmax )
829
839
(void )printf ("PING %s: (%d ... %d) data bytes\n" ,
@@ -974,8 +984,17 @@ main(argc, argv)
974
984
nmissedmax = ntransmitted - nreceived - 1 ;
975
985
if (options & F_MISSED )
976
986
(void )write (STDOUT_FILENO , & BBELL , 1 );
977
- if (!(options & F_QUIET ))
978
- printf ("Request timeout for icmp_seq %ld\n" , ntransmitted - 2 );
987
+ if (!(options & F_QUIET )) {
988
+ if (printmaxtime_ms > 0 ) {
989
+ char outstr [200 ];
990
+
991
+ if (!downsince )
992
+ downsince = time (NULL );
993
+ (void )printf ("\r%s (%s) DOWN: %ld s, total: %ld " ,
994
+ timebufnow (outstr ,sizeof (outstr ),downsince ), target , time (NULL )- downsince , nmissedmax ); fflush (stdout );
995
+ } else
996
+ printf ("Request timeout for icmp_seq %ld\n" , ntransmitted - 2 );
997
+ }
979
998
}
980
999
}
981
1000
}
@@ -984,6 +1003,16 @@ main(argc, argv)
984
1003
exit (0 ); /* Make the compiler happy */
985
1004
}
986
1005
1006
+ static char * timebufnow (char * outstr , int len , time_t now )
1007
+ {
1008
+ struct tm * tmp ;
1009
+
1010
+ if (!now ) now = time (NULL );
1011
+ tmp = localtime (& now );
1012
+ strftime (outstr , len , "%a, %d %b %Y %T %z" , tmp );
1013
+ return outstr ;
1014
+ }
1015
+
987
1016
/*
988
1017
* stopit --
989
1018
* Set the global bit that causes the main loop to quit.
@@ -1067,7 +1096,14 @@ pinger(void)
1067
1096
usleep (FLOOD_BACKOFF );
1068
1097
return ;
1069
1098
}
1070
- warn ("sendto" );
1099
+ if (printmaxtime_ms == 0 ) {
1100
+ warn ("sendto" );
1101
+ } else {
1102
+ if ((errno != 64 )&& (errno != 65 )) {
1103
+ fprintf (stderr ,"\nErrno: %d " , errno );
1104
+ warn ("sendto" );
1105
+ }
1106
+ }
1071
1107
} else {
1072
1108
warn ("%s: partial write: %d of %d bytes" ,
1073
1109
hostname , i , cc );
@@ -1098,7 +1134,7 @@ pr_pack(buf, cc, from, tv)
1098
1134
struct icmp * icp ;
1099
1135
struct ip * ip ;
1100
1136
const void * tp ;
1101
- double triptime ;
1137
+ double triptime = 0.0 ;
1102
1138
int dupflag , hlen , i , j , recv_len , seq ;
1103
1139
static int old_rrlen ;
1104
1140
static char old_rr [MAX_IPOPTLEN ];
@@ -1171,16 +1207,29 @@ pr_pack(buf, cc, from, tv)
1171
1207
1172
1208
if (options & F_FLOOD )
1173
1209
(void )write (STDOUT_FILENO , & BSPACE , 1 );
1174
- else {
1175
- (void )printf ("%d bytes from %s: icmp_seq=%u" , cc ,
1176
- inet_ntoa (* (struct in_addr * )& from -> sin_addr .s_addr ),
1177
- seq );
1178
- (void )printf (" ttl=%d" , ip -> ip_ttl );
1179
- if (timing )
1180
- (void )printf (" time=%.3f ms" , triptime );
1181
- if (dupflag ) {
1182
- if (!IN_MULTICAST (ntohl (whereto .sin_addr .s_addr )))
1183
- (void )printf (" (DUP!)" );
1210
+ else if ( triptime >= printmaxtime_ms ) {
1211
+ if (downsince > 0 ) {
1212
+ char outstr [200 ];
1213
+
1214
+ downtime += downsince ;
1215
+ printf ("\n%s (%s) UP. Down for %ld s, total downtime: %ld\n" , timebufnow (outstr , sizeof (outstr ), 0 ), target , time (NULL )- downsince , downtime );
1216
+ downsince = 0 ;
1217
+ }
1218
+ if (printmaxtime_ms ) {
1219
+ char t [200 ];
1220
+ (void )printf ("%s (%s) %.3f ms > %d.0 ms" ,
1221
+ timebufnow (t ,sizeof (t ),0 ), target , triptime , printmaxtime_ms );
1222
+ } else {
1223
+ (void )printf ("%d bytes from %s: icmp_seq=%u" , cc ,
1224
+ inet_ntoa (* (struct in_addr * )& from -> sin_addr .s_addr ),
1225
+ seq );
1226
+ (void )printf (" ttl=%d" , ip -> ip_ttl );
1227
+ if (timing )
1228
+ (void )printf (" time=%.3f ms" , triptime );
1229
+ if (dupflag ) {
1230
+ if (!IN_MULTICAST (ntohl (whereto .sin_addr .s_addr )))
1231
+ (void )printf (" (DUP!)" );
1232
+ }
1184
1233
}
1185
1234
if (options & F_AUDIBLE )
1186
1235
(void )write (STDOUT_FILENO , & BBELL , 1 );
@@ -1346,7 +1395,7 @@ pr_pack(buf, cc, from, tv)
1346
1395
(void )printf ("\nunknown option %x" , * cp );
1347
1396
break ;
1348
1397
}
1349
- if (!(options & F_FLOOD )) {
1398
+ if (!(options & F_FLOOD ) && ( triptime >= printmaxtime_ms ) ) {
1350
1399
(void )putchar ('\n' );
1351
1400
(void )fflush (stdout );
1352
1401
}
0 commit comments