-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodefileread.js
executable file
·199 lines (161 loc) · 5.66 KB
/
nodefileread.js
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
var fs = require('fs');
var jf = require('jsonfile')
var val = require('validator')
var mysql = require('mysql')
var $ = require('jquery')
var jsonObject=[];
var lineNumber=0;
var columns=[];
//Sid and Cid initialization
//var $ = require('jQuery')
// object for creating HighChart bar graph
var snortObject = {
"record": [
{
Id: 0,
"dataPayload": "",
"event": {
"signature": "",
"timestamp":""
},
"icmphdr": {
icmp_type: -1 ,
icmp_code: -1,
icmp_csum: -1 ,
icmp_id: -1,
icmp_seq:-1
},
"iphdr": {
ip_src: -1,
ip_dst: -1,
ip_ver: -1,
ip_hlen: -1,
ip_tos: -1,
ip_len: -1,
ip_id: -1,
ip_flags: -1,
ip_off: -1,
ip_ttl: -1,
ip_proto: -1,
ip_csum: -1
},
"sensor": {
"hostname": "" ,
"interface":""
},
"sig_class_name" : "" ,
"signature" : {
"sig_name": "",
sig_priority:-1 ,
},
"tcphdr": {
tcp_sport:-1 ,
tcp_dport: -1,
tpc_seq: -1,
tcp_ack: -1,
tcp_off: -1,
tcp_res: -1,
tcp_flags: -1,
tcp_win: -1,
tcp_csum: -1,
tcp_urp: -1
},
"udphdr": {
udp_sport: -1,
udp_dport: -1,
udp_len: -1,
udp_csum: -1
}
},
{
}
]};
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'admin'
});
connection.connect(function(err) {
console.log('Connected');// connected! (unless `err` is set)
});
connection.query("use snort");
//var Strquery = "select * from event natural join data natural join iphdr where sid=1 and cid =1";
//var Strquery = "select * from signature natural join event natural join iphdr natural join data natural join sig_class where event.signature = signature.sig_id and event.sid in (select 1 and event.cid = 1) and signature.sig_class_id= sig_class.sig_class_id order by event.sid";
//var Strquery = "select * from event natural join iphdr where sid > ;
//var sidCidQuery = "select distinct sid,cid from event";
//select sid,cid from event where sid in (select distinct(sid) from event)";
var strQuery = "select * from event e natural left join signature sig natural left join iphdr natural left join data natural left join icmphdr natural left join udphdr natural left join tcphdr where e.signature=sig.sig_id and ";
var maxSid;
var lastSid = 0;
var lastCid = 0;
var temp = [];
var numOfEvents;
var proto = [];
proto[1] = "icmphdr";
proto[17] = "udphdr";
proto[6]="tcphdr";
//var proto = { "1":"icmphdr","17":"udphdr","6":"tcphdr"};
/*function hello()
{
alert("Hello");
}
*/
function writeToJson( rows) {
var outputFilename = './my.json';
fs.writeFile(outputFilename, JSON.stringify(rows,null,4), function(err) {
if(err) {
console.log(err);
} else {
console.log("JSON saved to " + outputFilename);
}
});
}
function queryRecords(writeToJson) {
connection.query(strQuery + "(sid > " + lastSid + " or " + "cid > " + lastCid + ")", function(err, rows){
if(err){
throw err;
}
else{
if (rows.length) {
numOfEvents = rows.length;
console.log("num",numOfEvents);
console.log(rows[0]);
}
}
writeToJson(rows);
});
}
queryRecords(writeToJson);
jquery.getJSON('my.json', function(data) {
if(err) {
throw err;
}
console.log("Okay");
});
connection.end();
/*
var Lazy = require('lazy'),
lazy = new Lazy(fs.createReadStream('Book2.csv'))
.lines
.map(String)
.map(function(lineData){
var fields = lineData.trim().split(',', 11);
if(lineNumber == 0)
{
columns.push(fields[3]);
columns.push(fields[10]);
} else {
// var jsoninput = {
// MEM : fields[3],
// CPU : fields[10]
// };
// jsonObject.push(jsoninput);
//
chartObject.xAxis.categories.push(fields[10]);
chartObject.series[0].data.push(val.toFloat(fields[3]));
var file = 'data.json';
jf.writeFile(file, chartObject);
}
lineNumber = lineNumber + 1;
});
*/