-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions.php
295 lines (259 loc) · 10.1 KB
/
functions.php
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/**
* [array_search_partial busca un string en un valor de un array y devuelve el key]
* @param [type] $arr [description]
* @param [type] $keyword [description]
* @return [type] [description]
*/
function array_search_partial( $arr, $keyword ) {
foreach ( $arr as $index => $string ) {
if ( strpos( $keyword, $string ) !== FALSE )
return $index;
} //$arr as $index => $string
}
/**
* [partial_search_array Busca si existe parte de un string en un array]
* @param [type] $haystack [description]
* @param [type] $needle [description]
* @return [type] [description]
*/
function partial_search_array( $haystack, $needle ) {
foreach ( $haystack as $item ) {
if ( strpos( $item, $needle ) !== false ) {
return true;
} //strpos( $item, $needle ) !== false
} //$haystack as $item
return false;
}
/**
* [partial_search_array Busca si existe parte de un string en un array]
* @param [type] $haystack [description]
* @param [type] $needle [description]
* @return [type] [description]
*/
function partial_search_array_special( $haystack, $needle ) {
foreach ( $haystack as $item ) {
if ( count( $needle ) > count( $item ) ) { //busco item en needle
if ( strpos( $item, $needle ) !== false ) {
return true;
} //strpos( $item, $needle ) !== false
} //count( $needle ) > count( $item )
else { //busco needle en item
if ( strpos( $needle, $item ) !== false ) {
return true;
} //strpos( $needle, $item ) !== false
}
} //$haystack as $item
return false;
}
/**
* [get_total_rules_active Get the total record of active rules]
* @return [type] [description]
*/
function get_total_rules_active( ) {
global $db_;
$SQL = "SELECT count(*) as TOTAL FROM `block_queue`;";
if ( !$result = $db_->query( $SQL ) ) {
die( 'There was an error running the query [' . $db_->error . ']' );
} //!$result = $db_->query( $SQL )
$row = $result->fetch_assoc();
return $row[ TOTAL ];
}
/**
* [get_rules_db Get array with rules on DB]
* @return [type] [description]
*/
function get_rules_db( ) {
global $db_;
$SQL = "SELECT * FROM sigs_to_block order by sig_name ;";
if ( !$result = $db_->query( $SQL ) ) {
die( 'There was an error running the query [' . $db_->error . ']' );
} //!$result = $db_->query( $SQL )
while ( $row = $result->fetch_assoc() ) {
$array_tmp[ ] = $row;
} //$row = $result->fetch_assoc()
return $array_tmp;
}
/**
* [get_rules_db Get the info of signature]
* @return [type] [description]
*/
function get_signature_info_db( $sid = NULL ) {
if ( !$sid )
return false;
global $db_;
$SQL = "SELECT * FROM signature,event WHERE `sig_sid` = '$sid' AND sig_id=signature ORDER BY timestamp desc LIMIT 1;";
if ( !$result = $db_->query( $SQL ) ) {
die( 'There was an error running the query [' . $db_->error . ']' );
} //!$result = $db_->query( $SQL )
$row = $result->fetch_assoc();
return $row;
}
/**
*
* TODO: Mejorar consulta SQL
*
* [get_rules_db Get the header info of conextion]
* @return [type] [description]
*/
function get_header_info_db( $cid = NULL ) {
if ( !$cid )
return false;
global $db_;
$SQL = "SELECT *,inet_ntoa(ip_src) as ip_src,inet_ntoa(ip_dst) as ip_dst FROM `iphdr` WHERE `cid` = '$cid' LIMIT 1;";
if ( !$result = $db_->query( $SQL ) )
die( 'There was an error running the query [' . $db_->error . ']' );
//!$result = $db_->query( $SQL )
$row[ 'packet' ] = $result->fetch_assoc();
$SQL = "SELECT * FROM `udphdr` WHERE `cid` = '$cid' LIMIT 1;";
if ( !$result = $db_->query( $SQL ) )
die( 'There was an error running the query [' . $db_->error . ']' );
//!$result = $db_->query( $SQL )
if ( $result->num_rows )
$row[ 'port' ] = $result->fetch_assoc();
else {
$SQL = "SELECT * FROM `tcphdr` WHERE `cid` = '$cid' LIMIT 1;";
if ( !$result = $db_->query( $SQL ) )
die( 'There was an error running the query [' . $db_->error . ']' ); //!$result = $db_->query( $SQL )
$row[ 'port' ] = $result->fetch_assoc();
}
return $row;
}
function obtiene_server_status( ) {
// UPTIME
if ( false === ( $str = @file( "/proc/uptime" ) ) )
return false;
$str = explode( " ", implode( "", $str ) );
$str = trim( $str[ 0 ] );
$min = $str / 60;
$hours = $min / 60;
$days = floor( $hours / 24 );
$hours = floor( $hours - ( $days * 24 ) );
$min = floor( $min - ( $days * 60 * 24 ) - ( $hours * 60 ) );
if ( $days !== 0 )
$res[ 'uptime' ] = $days . " Days ";
if ( $hours !== 0 )
$res[ 'uptime' ] .= $hours . " Hours ";
$res[ 'server_uptime' ] .= $min . " Minutes ";
// MEMORY
if ( false === ( $str = @file( "/proc/meminfo" ) ) )
return false;
$str = implode( "", $str );
preg_match_all( "/MemTotal\s{0,}\:+\s{0,}([\d\.]+).+?MemFree\s{0,}\:+\s{0,}([\d\.]+).+?Cached\s{0,}\:+\s{0,}([\d\.]+).+?SwapTotal\s{0,}\:+\s{0,}([\d\.]+).+?SwapFree\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buf );
$res[ 'memTotal' ] = round( $buf[ 1 ][ 0 ], 2 );
$res[ 'memFree' ] = round( $buf[ 2 ][ 0 ], 2 );
$res[ 'memCached' ] = round( $buf[ 3 ][ 0 ], 2 );
$res[ 'memUsed' ] = ( $res[ 'memTotal' ] - $res[ 'memFree' ] );
$res[ 'memPercent' ] = ( floatval( $res[ 'memTotal' ] ) != 0 ) ? round( $res[ 'memUsed' ] / $res[ 'memTotal' ] * 100, 2 ) : 0;
$res[ 'memRealUsed' ] = ( $res[ 'memTotal' ] - $res[ 'memFree' ] - $res[ 'memCached' ] );
$res[ 'memRealPercent' ] = ( floatval( $res[ 'memTotal' ] ) != 0 ) ? round( $res[ 'memRealUsed' ] / $res[ 'memTotal' ] * 100, 2 ) : 0;
$res[ 'swapTotal' ] = round( $buf[ 4 ][ 0 ], 2 );
$res[ 'swapFree' ] = round( $buf[ 5 ][ 0 ], 2 );
$res[ 'swapUsed' ] = ( $res[ 'swapTotal' ] - $res[ 'swapFree' ] );
$res[ 'swapPercent' ] = ( floatval( $res[ 'swapTotal' ] ) != 0 ) ? round( $res[ 'swapUsed' ] / $res[ 'swapTotal' ] * 100, 2 ) : 0;
// LOAD AVG
if ( false === ( $str = @file( "/proc/loadavg" ) ) )
return false;
$str = explode( " ", implode( "", $str ) );
$str = array_chunk( $str, 4 );
$res[ 'loadAvg' ] = implode( " ", $str[ 0 ] );
$res[ 'memTotal' ] = filesize_format( $res[ 'memTotal' ] * 1024 );
$res[ 'memUsed' ] = filesize_format( $res[ 'memUsed' ] * 1024 );
$res[ 'memCached' ] = filesize_format( $res[ 'memCached' ] * 1024 );
$res[ 'swapTotal' ] = filesize_format( $res[ 'swapTotal' ] * 1024, '', 'GB' );
$res[ 'swapFree' ] = filesize_format( $res[ 'swapFree' ] * 1024 );
$res[ 'swapUsed' ] = filesize_format( $res[ 'swapUsed' ] * 1024 );
return $res;
}
// * Format a number of bytes into a human readable format.
// * Optionally choose the output format and/or force a particular unit
function filesize_format( $size, $level = 0, $precision = 2, $base = 1024 ) {
$unit = array(
'B',
'kB',
'MB',
'GB',
'TB',
'PB',
'EB',
'ZB',
'YB'
);
$times = floor( log( $size, $base ) );
return sprintf( "%." . $precision . "f", $size / pow( $base, ( $times + $level ) ) ) . " " . $unit[ $times + $level ];
}
//Facebook like
function format_fecha( $time ) {
if ( $time !== intval( $time ) ) {
$time = strtotime( $time );
} //$time !== intval( $time )
$d = time() - $time;
if ( $time < strtotime( date( 'Y-m-d 00:00:00' ) ) - 60 * 60 * 24 * 3 ) {
$format = 'F j';
if ( date( 'Y' ) !== date( 'Y', $time ) ) {
$format .= ", Y";
} //date( 'Y' ) !== date( 'Y', $time )
return date( $format, $time );
} //$time < strtotime( date( 'Y-m-d 00:00:00' ) ) - 60 * 60 * 24 * 3
if ( $d >= 60 * 60 * 24 ) {
$day = 'Ayer';
if ( date( 'l', time() - 60 * 60 * 24 ) !== date( 'l', $time ) ) {
$day = date( 'l', $time );
} //date( 'l', time() - 60 * 60 * 24 ) !== date( 'l', $time )
return $day . " a las " . date( 'g:ia', $time );
} //$d >= 60 * 60 * 24
if ( $d >= 60 * 60 * 2 ) {
return intval( $d / ( 60 * 60 ) ) . " hours ago";
} //$d >= 60 * 60 * 2
if ( $d >= 60 * 60 ) {
return "1 hour ago";
} //$d >= 60 * 60
if ( $d >= 60 * 2 ) {
return intval( $d / 60 ) . " minutes ago";
} //$d >= 60 * 2
if ( $d >= 60 ) {
return "a minute ago";
} //$d >= 60
if ( $d >= 2 ) {
return intval( $d ) . " seconds";
} //$d >= 2
return "a few seconds ago";
}
function get_server_uptime( ) {
$exec_uptime = preg_split( "/[\s]+/", trim( shell_exec( 'uptime' ) ) );
$uptime = $exec_uptime[ 2 ] . ' Days';
return $uptime;
}
function check_connect_router_API( ) {
global $router;
require( '/opt/ips-mikrotik-suricata/share/routeros_api.php' );
$API = new RouterosAPI();
if ( $API->connect( $router[ 'ip' ], $router[ 'user' ], $router[ 'pass' ] ) )
return "<span class='label label-success '>OK</span>";
else
return ( 'Unable to connect to RouterOS. Error:' . $e );
$API->disconnect();
}
function check_service_running( $service = "ids" ) {
global $PID_app_file;
if ( $service == "ids" )
$cmd = "suricata -c /etc/suricata/suricata.yaml";
elseif ( $service == "db" )
$cmd = "barnyard2 -c /etc/suricata/barnyard2.conf";
elseif ( $service == "snorby" )
$cmd = "ruby script/rails server -e production -d -b 0.0.0.0";
elseif ( $service == "ips" )
if ( file_exists( $PID_app_file ) )
return "<span class='label label-success'>OK</span>";
else
return "<span class='label label-danger'>NO</span>";
$cmd_exec = "ps ax | grep -v grep | grep '$cmd' | wc -l";
// echo $cmd_exec;
$ret = exec( $cmd_exec );
// return $ret;
if ( $ret )
return "<span class='label label-success '>OK</span>";
else
return "<span class='label label-danger lead'>NO</span>";
}
?>