#include "../include/traffic.h"
void write_line(struct host *h, u_char type, FILE *fp);
void
log_file()
{
FILE *fp; /* File pointer */
u_int i; /* Counter variable */
struct host *host;
/* Open logfile */
if ( (fp = fopen(opt.logfile, "w")) == NULL ) {
perror(opt.logfile);
exit(1);
}
/* Write header */
fprintf( fp, "Traffic Analyzer (c) Krzysztof Pawlowski 2002, 2003\n\n");
for(i=0; i < MAX_COL; i++)
switch(opt.columns[i]) {
case COL_IP:
fprintf( fp, "%15s ","Host");
break;
case COL_DATA:
fprintf( fp, "%19s ","Data");
break;
case COL_DATA_MAX:
fprintf( fp, "%19s ","Data max");
break;
case COL_DATA_AVG:
fprintf( fp, "%19s ","Data avg");
break;
case COL_DATA_TOT:
fprintf( fp, "%23s ","Data total");
break;
case COL_PKT:
fprintf( fp, "%13s ","Packets");
break;
case COL_PKT_TOT:
fprintf( fp, "%17s ","Packets total");
break;
case COL_PKT_AVG:
fprintf( fp, "%13s ","Packets avg");
break;
case COL_PKT_MAX:
fprintf( fp, "%13s ","Packets max");
break;
case COL_MAC:
fprintf( fp, "%17s ","MAC address");
break;
case COL_HOSTNAME:
fprintf( fp, "%32s ","Hostname");
break;
case COL_TCP:
fprintf( fp, "%19s ","TCP");
break;
case COL_UDP:
fprintf( fp, "%19s ","UDP");
break;
case COL_ICMP:
fprintf( fp, "%19s ","ICMP");
break;
case COL_TCP_TOT:
fprintf( fp, "%23s ","Total TCP");
break;
case COL_UDP_TOT:
fprintf( fp, "%23s ","Total UDP");
break;
case COL_ICMP_TOT:
fprintf( fp, "%23s ","Total ICMP");
break;
}
/* Print statistics to file */
for(i=0; i < h_num; i++)
write_line(&hosts[i], 0, fp);
if (opt.total) {
fprintf(fp, "\n");
write_line(&total,1, fp);
}
/* Print footer */
fprintf( fp, "\n\nDevice: %s | Unit: %s / %s | Refresh: %ds | Time: %2dh %2dm %2ds | From / To"
, opt.dev, opt.unit_name, opt.unit_t_name, opt.delay
, opt.times * opt.delay / 3600
, ((opt.times * opt.delay) % 3600) / 60
, (opt.times * opt.delay) % 60 );
fclose(fp);
}
/* Write line with statisic of one host */
void write_line(struct host *h, u_char type, FILE *fp)
{
int i;
/* Write IP */
if (type == 0)
fprintf( fp, "\n%-15s ", inet_ntoa(h->ip));
else
fprintf( fp, "\n%-15s ", "TOTAL");
for(i=1; i < MAX_COL; i++)
switch(opt.columns[i]) {
case COL_DATA:
/* Write data current */
fprintf( fp, "%8.2f / %8.2f ", (h->last_in * opt.unit / opt.delay)
, (h->last_out * opt.unit / opt.delay));
break;
case COL_DATA_MAX:
/* Write data max */
fprintf( fp, "%8.2f / %8.2f ", (h->max_in * opt.unit / opt.delay)
, (h->max_out * opt.unit / opt.delay));
break;
case COL_DATA_AVG:
/* Write data average */
fprintf( fp, "%8.2f / %8.2f ", (h->tot_in * opt.unit / (opt.times * opt.delay))
, (h->tot_out * opt.unit / (opt.times * opt.delay)));
break;
case COL_DATA_TOT:
/* Write data total */
fprintf( fp, "%10.2f / %10.2f ", (h->tot_in * opt.unit_t)
, (h->tot_out * opt.unit_t));
break;
case COL_PKT:
/* Write packet current */
fprintf( fp, "%5d / %5d ", (int)(h->last_pkt_in / opt.delay)
, (int)(h->last_pkt_out / opt.delay));
break;
case COL_PKT_TOT:
/* Write packet total */
fprintf( fp, "%8d / %8d", (int)h->tot_pkt_in
, (int)h->tot_pkt_out);
break;
case COL_PKT_AVG:
/* Write packet average */
fprintf( fp, "%5d / %5d ", (int)(h->tot_pkt_in / (opt.delay * opt.times))
, (int)(h->tot_pkt_out / (opt.delay * opt.times)));
break;
case COL_PKT_MAX:
/* Write packet maximum */
fprintf( fp, "%5d / %5d ", (int)(h->max_pkt_in / opt.delay)
, (int)(h->max_pkt_out / opt.delay));
break;
case COL_MAC:
/* Write MAC address */
fprintf( fp, "%17s", h->mac);
break;
case COL_HOSTNAME:
/* Write hostname */
fprintf( fp, "%32s", h->hostname);
break;
case COL_TCP:
/* Write TCP current */
fprintf( fp, "%8.2f / %8.2f ", (h->last_tcp_in * opt.unit / opt.delay)
, (h->last_tcp_out * opt.unit / opt.delay));
break;
case COL_UDP:
/* Write UDP current */
fprintf( fp, "%8.2f / %8.2f ", (h->last_udp_in * opt.unit / opt.delay)
, (h->last_udp_out * opt.unit / opt.delay));
break;
case COL_ICMP:
/* Write ICMP current */
fprintf( fp, "%8.2f / %8.2f ", (h->last_icmp_in * opt.unit / opt.delay)
, (h->last_icmp_out * opt.unit / opt.delay));
break;
case COL_TCP_TOT:
/* Write TCP total */
fprintf( fp, "%10.2f / %10.2f ", (h->tot_tcp_in * opt.unit_t)
, (h->tot_tcp_out * opt.unit_t));
break;
case COL_UDP_TOT:
/* Write UDP total */
fprintf( fp, "%10.2f / %10.2f ", (h->tot_udp_in * opt.unit_t)
, (h->tot_udp_out * opt.unit_t));
break;
case COL_ICMP_TOT:
/* Write ICMP total */
fprintf( fp, "%10.2f / %10.2f ", (h->tot_icmp_in * opt.unit_t)
, (h->tot_icmp_out * opt.unit_t));
break;
}
}
syntax highlighted by Code2HTML, v. 0.9.1