File: convertLog.cc

package info (click to toggle)
hammerhead 2.1.3-9.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 716 kB
  • ctags: 588
  • sloc: cpp: 4,786; sh: 2,917; makefile: 248; perl: 41; csh: 11
file content (266 lines) | stat: -rw-r--r-- 6,735 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

//
// $Id: convertLog.cc,v 1.2 2002/11/30 01:38:45 dredd Exp $
//
// $Source: /cvsroot/hammerhead/hammerhead/utils/convertLog.cc,v $
// $Revision: 1.2 $
// $Date: 2002/11/30 01:38:45 $
// $State: Exp $
//
// Author: Jon Gifford
//
// Purpose: Convert a log file into a set of scenarios
//

#include <time.h>
#include <stdio.h>
#include <iostream.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "dictionary.h"
#include "str.h"

#ifndef SunOS
#include "hrtime.h"
#endif

extern char * strptime (const char * buf, const char * format, struct tm * tm);

class knownIp 
{
public: 
    String ipAddress;
    int ipCount;
    time_t ipTime;
    String ipRequest;
    long scenarioNum;
};
    
void destroy(knownIp **) { }
#define MAXTHINK 60

void
printScenario(FILE *outF, const String& ipA, const String& ipR, long thinkT, int cnt,
              long scenarioNum, long nextScenarioNum)
{
#if 0
    struct in_addr ipAddr;

#ifdef SunOS
    ipAddr.S_un.S_addr = ipA;
#else
    ipAddr.s_addr = ipA;
#endif
#endif

    fprintf(outF, "#%s #%d\nN%lx\nR%s\n", 
            ipA.c_str(), cnt, scenarioNum, ipR.c_str());
    if (thinkT >= 0 && thinkT < MAXTHINK)
    {
        fprintf(outF, "S%lx\nT%ld\n", 
                nextScenarioNum, thinkT * 1000000);
    }
    fprintf(outF, "X%d\n.\n", (scenarioNum == -1) ? 0 : 1);
}


main(int argc, char *argv[])
{
    if (argc != 3)
    {
        cerr << "Usage: " << argv[0] << " logFileName scenarioFileName\n";
        exit(1);
    }
    
    FILE *inFile;
    if (argv[1][0] == '-')
    {
        // stdin redirect
        inFile = stdin;
    }
    else
    {
        inFile = fopen(argv[1], "r");
        if (inFile == NULL)
        {
            cerr << "Error: Unable to open log file: " << argv[1] << endl;
            exit(2);
        }
    }

    FILE *outFile;
    if (argv[2][0] == '-')
    {
        // stdout
        outFile = stdout;
    }
    else
    {
        outFile = fopen(argv[2], "w");
        if (outFile == NULL)
        {
            cerr << "Error: unable to open scenario file: " << argv[2] << endl;
            exit(3);
        }
    }

    Dictionary<knownIp *> knownIps;

    int lineNum = 0;
    int created = 0;
    int longestSeq = 0;

    hrtime_t fileStart = gethrtime();
    char inBuf[BUFSIZ];
    while (fgets(inBuf, BUFSIZ, inFile))
    {
        lineNum++;
        if (lineNum % 5000 == 0)
        {
            hrtime_t currT = gethrtime();
            double elapsed = (currT - fileStart)/1e9;
            double scenPerSec = lineNum/elapsed;
            fprintf(stderr, " %d (%d,%.0f,%.0f)", 
                    lineNum, knownIps.num_elements(),
                    elapsed, scenPerSec);
        }

        inBuf[strlen(inBuf) - 1] = '\0';
        if (strlen(inBuf) == 0)
        {
            // blank line
            continue;
        }

        char *ipStr = strtok(inBuf, " ");
        String source = ipStr;
#if 0
        // resolve it?
        unsigned long source = inet_addr(ipStr);

        if ((long )source == -1 || (long )source == 0)
        {
            cerr << "\nWarning: illegal ip address " << ipStr
                 << " - Ignoring line " << lineNum << "\n";
            continue;
        }
#endif

        // now parse the rest of the line...
        char *tempToken = strtok(0, "[");
        char *time = strtok(0, "]");
        if (time == 0)
        {
            cerr << "\nWarning: No Date/Time string found"
                 << " - Ignoring line " << lineNum << "\n";
            continue;
        }

        tempToken = strtok(0, "\"");
        char *request = strtok(0, "\"");
        if (request == 0)
        {
            cerr << "\nWarning: No request string found"
                 << " - Ignoring line " << lineNum << "\n";
            continue;
        }

        struct tm cTm;
        char *nextC = strptime(time, "%d/%b/%Y:%T", &cTm);

        if (nextC == 0)
        {
            cerr << "\nWarning: Illegal Date/Time string " << time
                 << " - Ignoring line " << lineNum << "\n";
            continue;
        }
        time_t cTime = mktime(&cTm);

        knownIp *ip;
        if (knownIps.exists(ipStr))
        {
            ip = knownIps[ipStr];

            long deltaT = cTime - ip->ipTime;
            printScenario(outFile, ip->ipAddress, ip->ipRequest, 
                          deltaT, ip->ipCount, ip->scenarioNum, lineNum);
            if (deltaT > MAXTHINK)
            {
                // assume new connection if think time too long...
                // initial request to / is not being logged, so fake it
                // whenever a new ip address is found.
                printScenario(outFile, ip->ipAddress, "GET / HTTP/1.0",
                              0, ip->ipCount, -1, lineNum);
            }
            ip->ipCount++;
        }
        else
        {
            ip = new knownIp();
            knownIps[ipStr] = ip;
            
            ip->ipAddress = source;
            ip->ipCount = 1;

            // initial request to / is not being logged, so fake it
            // whenever a new ip address is found.
            printScenario(outFile, ip->ipAddress, "GET / HTTP/1.0",
                          0, 0, -1, lineNum);
        }

        ip->ipTime = cTime;
        ip->ipRequest = request;
        ip->scenarioNum = lineNum;

        created++;
    }

    knownIp **allElements = knownIps.element_array();
    for (int i = 0; i < knownIps.num_elements(); i++)
    {
        knownIp *ip = allElements[i];
        printScenario(outFile, ip->ipAddress, ip->ipRequest, -1, 
                      ip->ipCount, ip->scenarioNum, 0);
        if (ip->ipCount > longestSeq)
        {
            longestSeq = ip->ipCount;
        }
    }

    cerr << "\n\nSUMMARY: Scenarios created\t" << created
         << "\n\t Input lines discarded\t" << lineNum - created
         << "\n\t Unique IP addresses\t" << knownIps.size()
         << "\n\t Longest Sequence\t" << longestSeq
         << "\n\t Sequences:\n\t\tLength\tNumber\t%";

    
    int arrayEnd = knownIps.num_elements();
    for (int j = 1; j <= longestSeq; j++) 
    {
        int cnt = 0;
        for (int i = 0; i < arrayEnd; )
        {
            knownIp *ip = allElements[i];
            if (ip->ipCount == j)
            {
                cnt++;
                allElements[i] = allElements[--arrayEnd];
                delete ip;
            }
            else
                i++;
        }
        if (cnt > 0)
        {
            cerr << "\n\t\t" << j << "\t" << cnt << "\t" 
                 << (int )(((double)cnt*j/(double)created)*100000)/1000.0;
        }
    }
    delete allElements;
    cerr << "\n\n";
}