~ubuntu-branches/ubuntu/maverick/ntop/maverick

« back to all changes in this revision

Viewing changes to ntop/intop/uptime.c

  • Committer: Bazaar Package Importer
  • Author(s): Dennis Schoen
  • Date: 2002-04-12 11:38:47 UTC
  • Revision ID: james.westby@ubuntu.com-20020412113847-4k4yydw0pzybc6g8
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 
3
 * uptime.c - Tell how long the program has been running.
 
4
 *
 
5
 * Luca Deri     <deri@ntop.org>
 
6
 * Rocco Carbone <rocco@ntop.org>
 
7
 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU General Public License as published by
 
11
 * the Free Software Foundation; either version 2 of the License, or
 
12
 * (at your option) any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License
 
20
 * along with this program; if not, write to the Free Software
 
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
22
 */
 
23
 
 
24
 
 
25
/*
 
26
 * ntop header file(s)
 
27
 */
 
28
#include "ntop.h"
 
29
 
 
30
#include "intop.h"
 
31
 
 
32
 
 
33
/*
 
34
 * Days In Seconds.
 
35
 */
 
36
static int days (time_t t1, time_t t2)
 
37
{
 
38
  return ((t2 - t1) / SECS_PER_DAY);
 
39
}
 
40
 
 
41
 
 
42
/*
 
43
 * Hours In Seconds.
 
44
 */
 
45
static int hours (time_t t1, time_t t2)
 
46
{
 
47
  return ((t2 - t1 - (days (t1, t2) * SECS_PER_DAY)) / SECS_PER_HOUR);
 
48
}
 
49
 
 
50
 
 
51
/*
 
52
 * Minutes In Seconds.
 
53
 */
 
54
static int mins (time_t t1, time_t t2)
 
55
{
 
56
  return ((t2 - t1 - (days (t1, t2) * SECS_PER_DAY) -
 
57
           (hours (t1, t2) * SECS_PER_HOUR)) / SECS_PER_MIN);
 
58
}
 
59
 
 
60
 
 
61
/*
 
62
 * Tell how long the program has been running.
 
63
 */
 
64
int intop_uptime (int argc, char * argv [])
 
65
{
 
66
  /*
 
67
   * Notice the command name.
 
68
   */
 
69
  char * commandname = argv [0];
 
70
 
 
71
  int c;
 
72
#define USAGE(xxx) \
 
73
  printf ("Usage: %s [-h]\n\
 
74
           \n", xxx)
 
75
  char * optstring = "h";
 
76
 
 
77
  /*
 
78
   * Reserve here space for the local variables.
 
79
   */
 
80
  time_t now = NOW;
 
81
  struct tm * tm = localtime (& now);
 
82
  NtopInterface * n;
 
83
 
 
84
  optind = 0;
 
85
  optarg = NULL;
 
86
 
 
87
  /*
 
88
   * Parse command line options to the application via standard system calls.
 
89
   */
 
90
  while ((c = getopt (argc, argv, optstring)) != -1)
 
91
    {
 
92
      switch (c)
 
93
        {
 
94
        case 'h':
 
95
          USAGE (commandname);
 
96
          return (0);
 
97
 
 
98
        default:
 
99
          USAGE (commandname);
 
100
          return (-1);
 
101
        }
 
102
    }
 
103
 
 
104
  if (optind < argc)
 
105
    {
 
106
      printf ("\nWrong option(s): \" ");
 
107
      while (optind < argc)
 
108
        printf ("%s ", argv [optind ++]);
 
109
      printf ("\"\n");
 
110
      USAGE (commandname);
 
111
      printf ("\n");
 
112
      return (-1);
 
113
    }
 
114
 
 
115
  printf ("%2d:%02d%s   up %3d days, %2d:%02d:%02d,    %d interface%s\n\n",
 
116
          tm -> tm_hour % 12,
 
117
          tm -> tm_min,
 
118
          tm -> tm_hour > 12 ? "pm" : "am",
 
119
          days (boottime, now),
 
120
          hours (boottime, now),
 
121
          mins (boottime, now),
 
122
          (int) (now - boottime) % 60,
 
123
          netno (), netno () > 1 ? "s" : "");
 
124
 
 
125
  while ((n = netnext()))
 
126
    printf ("(%s) -- %s [%s],   %d hosts,   %s Pkts / %s\n",
 
127
            n -> name, n -> fqdn, n -> ipdot,
 
128
            n -> hostsno,
 
129
            cformatPkts (n -> ethernetPkts),
 
130
            cformatBytes (n -> ethernetBytes));
 
131
 
 
132
  fflush (stdout);
 
133
 
 
134
  return (0);
 
135
}