~ubuntu-branches/ubuntu/natty/ntop/natty

« back to all changes in this revision

Viewing changes to dataFormat.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-01-30 21:59:13 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050130215913-xc3ke963bw49b3k4
Tags: 2:3.0-5
* Updated README.Debian file so users will understand what to do at
  install, closes: #291794, #287802.
* Updated ntop init script to give better output.
* Also changed log directory from /var/lib/ntop to /var/log/ntop,
  closes: #252352.
* Quoted the interface list to allow whitespace, closes: #267248.
* Added a couple of logcheck ignores, closes: #269321, #269319.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 1998-2004 Luca Deri <deri@ntop.org>
 
3
 *
 
4
 *                          http://www.ntop.org/
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 */
 
20
 
 
21
#include "ntop.h"
 
22
 
 
23
 
 
24
/* ****************************************** */
 
25
 
 
26
char* formatKBytes(float numKBytes, char *outStr, int outStrLen) {
 
27
  if(numKBytes < 0) return(""); /* It shouldn't happen */
 
28
 
 
29
  if(numKBytes < 1024) {
 
30
    if(snprintf(outStr, outStrLen, "%.1f%sKB", numKBytes, myGlobals.separator) < 0) 
 
31
     BufferTooShort();
 
32
  } else {
 
33
    float tmpKBytes = numKBytes/1024;
 
34
 
 
35
    if(tmpKBytes < 1024) {
 
36
      if(snprintf(outStr, outStrLen, "%.1f%sMB",  tmpKBytes, myGlobals.separator) < 0) 
 
37
        BufferTooShort();
 
38
    } else {
 
39
      float tmpGBytes = tmpKBytes/1024;
 
40
 
 
41
      if(tmpGBytes < 1024) {
 
42
        if(snprintf(outStr, outStrLen, "%.1f%sGB", tmpGBytes, myGlobals.separator)  < 0) 
 
43
         BufferTooShort();
 
44
      } else {
 
45
        if(snprintf(outStr, outStrLen, "%.1f%sTB", ((float)(tmpGBytes)/1024), myGlobals.separator) < 0) 
 
46
         BufferTooShort();
 
47
      }
 
48
    }
 
49
  }
 
50
 
 
51
  return(outStr);
 
52
}
 
53
 
 
54
/* ******************************* */
 
55
 
 
56
char* formatBytes(Counter numBytes, short encodeString, char* outStr, int outStrLen) {
 
57
  char* locSeparator;
 
58
 
 
59
  if(encodeString)
 
60
    locSeparator = myGlobals.separator;
 
61
  else
 
62
    locSeparator = " ";
 
63
 
 
64
  if(numBytes == 0) {
 
65
    return("0"); /* return("&nbsp;"); */
 
66
  } else if(numBytes < 1024) {
 
67
    if(snprintf(outStr, outStrLen, "%lu", (unsigned long)numBytes) < 0) 
 
68
     BufferTooShort();
 
69
  } else if (numBytes < 1048576) {
 
70
    if(snprintf(outStr, outStrLen, "%.1f%sKB",
 
71
                ((float)(numBytes)/1024), locSeparator) < 0) 
 
72
     BufferTooShort();
 
73
  } else {
 
74
    float tmpMBytes = ((float)numBytes)/1048576;
 
75
 
 
76
    if(tmpMBytes < 1024) {
 
77
      if(snprintf(outStr, outStrLen, "%.1f%sMB",
 
78
              tmpMBytes, locSeparator) < 0) 
 
79
        BufferTooShort();
 
80
    } else {
 
81
      tmpMBytes /= 1024;
 
82
 
 
83
      if(tmpMBytes < 1024) {
 
84
        if(snprintf(outStr, outStrLen, "%.1f%sGB", tmpMBytes, locSeparator) < 0) 
 
85
         BufferTooShort();
 
86
      } else {
 
87
        if(snprintf(outStr, outStrLen, "%.1f%sTB",
 
88
                ((float)(tmpMBytes)/1024), locSeparator) < 0)
 
89
         BufferTooShort();
 
90
      }
 
91
    }
 
92
  }
 
93
 
 
94
  return(outStr);
 
95
}
 
96
 
 
97
/* ******************************* */
 
98
 
 
99
char* formatAdapterSpeed(Counter numBits, char* outStr, int outStrLen) {
 
100
  if(numBits == 0) {
 
101
    return("0"); /* return("&nbsp;"); */
 
102
  } else if(numBits < 1000) {
 
103
    if(snprintf(outStr, outStrLen, "%lu", (unsigned long)numBits) < 0) 
 
104
     BufferTooShort();
 
105
  } else if(numBits < 1000000) {
 
106
    if(snprintf(outStr, outStrLen, "%.1f Kb", (float)(numBits)/1000) < 0) 
 
107
     BufferTooShort();
 
108
  } else {
 
109
    float tmpMBytes = ((float)numBits)/1000000;
 
110
 
 
111
    if(tmpMBytes < 1000) {
 
112
      if(snprintf(outStr, outStrLen, "%.1f Mb", tmpMBytes) < 0) 
 
113
        BufferTooShort();
 
114
    } else {
 
115
      tmpMBytes /= 1000;
 
116
 
 
117
      if(tmpMBytes < 1000) {
 
118
        if(snprintf(outStr, outStrLen, "%.1f Gb", tmpMBytes) < 0) 
 
119
         BufferTooShort();
 
120
      } else {
 
121
        if(snprintf(outStr, outStrLen, "%.1f Tb", ((float)(tmpMBytes)/1000)) < 0)
 
122
         BufferTooShort();
 
123
      }
 
124
    }
 
125
  }
 
126
 
 
127
  return(outStr);
 
128
}
 
129
 
 
130
/* ******************************* */
 
131
 
 
132
char* formatSeconds(unsigned long sec, char* outStr, int outStrLen) {
 
133
  unsigned int hour=0, min=0, days=0;
 
134
 
 
135
  if(sec >= 3600) {
 
136
    hour = (sec / 3600);
 
137
 
 
138
    if(hour > 0) {
 
139
      if(hour >= 24) {
 
140
        days = (hour / 24);
 
141
        hour = hour % 24;
 
142
        sec -= days*86400;
 
143
      }
 
144
      sec -= hour*3600;
 
145
    } else
 
146
      hour = 0;
 
147
  }
 
148
 
 
149
  min = (sec / 60);
 
150
  if(min > 0) sec -= min*60;
 
151
 
 
152
  if(days > 0) {
 
153
    if(snprintf(outStr, outStrLen, "%u day%s %u:%02u:%02lu", days, (days>1)?"s":"", hour, min, sec) < 0) 
 
154
     BufferTooShort();
 
155
  } else if(hour > 0) {
 
156
    if(snprintf(outStr, outStrLen, "%u:%02u:%02lu", hour, min, sec)  < 0) 
 
157
     BufferTooShort();
 
158
  } else if(min > 0) {
 
159
    if(snprintf(outStr, outStrLen, "%u:%02lu", min, sec) < 0) 
 
160
     BufferTooShort();
 
161
  } else {
 
162
    if(snprintf(outStr, outStrLen, "%lu sec", sec) < 0)
 
163
     BufferTooShort();
 
164
  }
 
165
 
 
166
  return(outStr);
 
167
}
 
168
 
 
169
/* ******************************* */
 
170
 
 
171
char* formatMicroSeconds(unsigned long microsec, 
 
172
                         char* outStr, int outStrLen) {
 
173
  float f = ((float)microsec)/1000;
 
174
 
 
175
  if(f < 1000) {
 
176
    if(snprintf(outStr, outStrLen, "%.1f ms", f) < 0) 
 
177
     BufferTooShort();
 
178
  } else {
 
179
    if(snprintf(outStr, outStrLen, "%.1f sec", (f/1000))  < 0) 
 
180
     BufferTooShort();
 
181
  } 
 
182
  return(outStr);
 
183
}
 
184
 
 
185
/* ******************************* */
 
186
 
 
187
char* formatThroughput(float numBytes /* <=== Bytes/second */, u_char htmlFormat,
 
188
                       char* outStr, int outStrLen) {
 
189
  float numBits;
 
190
  int divider = 1000;   /* As SNMP does instead of using 1024 ntop divides for 1000 */
 
191
  char *separator;
 
192
 
 
193
  if(htmlFormat)
 
194
    separator = myGlobals.separator;
 
195
  else
 
196
    separator = " ";
 
197
  
 
198
  if(numBytes < 0) numBytes = 0; /* Sanity check */
 
199
  numBits = numBytes*8;
 
200
 
 
201
  if (numBits < 100)
 
202
    numBits = 0; /* Avoid very small decimal values */
 
203
  
 
204
  if (numBits < divider) {
 
205
    if(snprintf(outStr, outStrLen, "%.1f%sbps", numBits, separator) < 0) 
 
206
     BufferTooShort();
 
207
  } else if (numBits < (divider*divider)) {
 
208
    if(snprintf(outStr, outStrLen, "%.1f%sKbps", ((float)(numBits)/divider), separator) < 0) 
 
209
     BufferTooShort();
 
210
  } else {
 
211
    if(snprintf(outStr, outStrLen, "%.1f%sMbps", ((float)(numBits)/1048576), separator) < 0) 
 
212
     BufferTooShort();
 
213
  }
 
214
 
 
215
#ifdef DEBUG
 
216
  traceEvent(CONST_TRACE_INFO, "%.2f = %s", numBytes, outStr);
 
217
#endif
 
218
 
 
219
  return(outStr);
 
220
}
 
221
 
 
222
/* ******************************* */
 
223
 
 
224
char* formatLatency(struct timeval tv, u_short sessionState, char* outStr, int outStrLen) {  
 
225
  if(((tv.tv_sec == 0) && (tv.tv_usec == 0)) 
 
226
     || (sessionState < FLAG_STATE_ACTIVE) 
 
227
     /* Patch courtesy of  
 
228
        Andreas Pfaller <apfaller@yahoo.com.au>
 
229
     */) {
 
230
    /* 
 
231
       Latency not computed (the session was initiated
 
232
       before ntop started 
 
233
    */
 
234
    return("&nbsp;");
 
235
  } else {
 
236
    if(snprintf(outStr, outStrLen, "%.1f&nbsp;ms",
 
237
            (float)(tv.tv_sec*1000+(float)tv.tv_usec/1000)) < 0)
 
238
      BufferTooShort();
 
239
    return(outStr);
 
240
  }
 
241
}
 
242
 
 
243
/* ******************************* */
 
244
 
 
245
char* formatTimeStamp(unsigned int ndays,
 
246
                      unsigned int nhours,
 
247
                      unsigned int nminutes, char* outStr, int outStrLen) {
 
248
  time_t theTime;
 
249
 
 
250
  /* printf("%u - %u - %u\n", ndays, nhours, nminutes); */
 
251
 
 
252
  if((ndays == 0)
 
253
     && (nhours == 0)
 
254
     && (nminutes == 0))
 
255
    return("now");
 
256
  else {
 
257
    theTime = myGlobals.actTime-(ndays*86400)-(nhours*3600)-(nminutes*60);
 
258
    strncpy(outStr, ctime(&theTime), outStrLen);
 
259
    outStr[outStrLen-1] = '\0'; /* Remove trailer '\n' */
 
260
    return(outStr);
 
261
  }
 
262
}
 
263
 
 
264
/* ************************ */
 
265
 
 
266
char* formatPkts(Counter pktNr, char* outStr, int outStrLen) {
 
267
  if(pktNr < 1000) {
 
268
    if(snprintf(outStr, outStrLen, "%lu", (unsigned long)pktNr) < 0) 
 
269
     BufferTooShort();
 
270
  } else if(pktNr < 1000000) {
 
271
    if(snprintf(outStr, outStrLen, "%lu,%03lu",
 
272
            (unsigned long)(pktNr/1000),
 
273
            ((unsigned long)pktNr)%1000) < 0) 
 
274
     BufferTooShort();
 
275
  } else if(pktNr < 1000000000) {
 
276
    unsigned long a, b, c;
 
277
    a = (unsigned long)(pktNr/1000000);
 
278
    b = (unsigned long)((pktNr-a*1000000)/1000);
 
279
    c = ((unsigned long)pktNr)%1000;
 
280
    if(snprintf(outStr, outStrLen, "%lu,%03lu,%03lu", a, b, c) < 0) 
 
281
     BufferTooShort();
 
282
  } else {
 
283
    unsigned long a, b, c, a1, a2;
 
284
    a = (unsigned long)(pktNr/1000000);
 
285
    a1 = (unsigned long)(a/1000); 
 
286
    a2 = a % 1000;
 
287
    b = (unsigned long)((pktNr-a*1000000)/1000);
 
288
    c = ((unsigned long)pktNr)%1000;
 
289
    if(snprintf(outStr, outStrLen, "%lu,%03lu,%03lu,%03lu", a1, a2, b, c) < 0) 
 
290
     BufferTooShort();
 
291
  }
 
292
 
 
293
  return(outStr);
 
294
}
 
295
 
 
296
/* ************************************ */
 
297
 
 
298
char* formatTime(time_t *theTime, char* outStr, int outStrLen) {
 
299
  struct tm *locTime;
 
300
  struct tm myLocTime;
 
301
 
 
302
  locTime = localtime_r(theTime, &myLocTime);
 
303
  strftime(outStr, outStrLen, CONST_LOCALE_TIMESPEC, locTime);
 
304
 
 
305
  return(outStr);
 
306
}
 
307