-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkilla.pl
129 lines (110 loc) · 2.4 KB
/
killa.pl
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
#!/usr/bin/perl
use strict;
use warnings;
chdir '/opt/ONS/';
umask 0;
use lib "Uniscan";
use Uniscan::Functions;
my $func = Uniscan::Functions->new();
my $ret = `ps --no-header aux | grep ONS |grep -v grep |grep -v killa`;
my $linha = "iniciando em: " . `date`;
my $a;
open($a, ">>/var/log/killons.log");
print $a $linha;
close($a);
while($ret =~m/(.+?)\s+(.+?)\s+(.+?)\s+(.+?)\s+(.+?)\s+(.+?)\s+(.+?)\s+(.+?)\s+(.+?)\s+(.+?)\s+(.+?)\n/g){
my $pid = $2;
my $proc = $11;
my $inicio = $9;
if($inicio =~/[A-Za-z]/){
my $hora = `date "+%H"`;
chomp $hora;
my $id = 0;
(undef, undef, $id) = split(' ', $proc) if($proc =~/perl/);
if($id != 0){
&killpid($pid) if($hora > 3 && $func->check_waf($id) == 1);
}
&killpid($pid) if($hora > 0 && $proc =~/^python/);
if($proc =~/^python/){
if(&existe($pid) == 1){
&killpid($pid);
&deletapid($pid);
}
else{
&addpid($pid);
}
}
}
else{
my ($inicio, undef) = split(':', $9);
my $hora = `date "+%H"`;
chomp $hora;
$hora += 23 if($hora < $inicio);
my $id = 0;
(undef, undef, $id) = split(' ', $proc) if($proc =~/perl/);
# print "id: $id\n$ret\n";
if($id != 0){
if(($hora - $inicio) > 3 && $func->check_waf($id) == 1 ){
&killpid($pid);
}
}
if(($hora - $inicio) > 0 && $proc =~/^python/){
&killpid($pid);
}
if($proc =~/^python/){
if(&existe($pid) == 1){
&killpid($pid);
&deletapid($pid);
}
else{
&addpid($pid);
}
}
}
}
exit(0);
sub killpid(){
my $pid = shift;
my $cmd = `ps aux |grep $pid |grep -v grep`;
$cmd .= " Morto em: ". `date`;
$cmd =~s/\n//g;
my $a;
open($a, ">>/var/log/killons.log");
print $a "$cmd\n";
close($a);
system("kill -9 $pid");
print "$cmd\n";
}
sub addpid(){
my $pid = shift;
open(my $arq, ">>/opt/ONS/killa.pids");
print $arq "$pid\n";
close($arq);
}
sub deletapid(){
my $pid = shift;
my @pids = ();
open(my $arq, "</opt/ONS/killa.pids");
while(<$arq>){
my $pidd = $_;
chomp $pidd;
push(@pids, $pidd) if($pid != $pidd);
}
close($arq);
open($arq, ">/opt/ONS/killa.pids");
foreach $pid (@pids){
print $arq "$pid\n";
}
close($arq);
}
sub existe(){
my $pid = shift;
open(my $arq, "</opt/ONS/killa.pids");
my @pids = <$arq>;
close($arq);
foreach my $pidd (@pids){
chomp $pidd;
return 1 if($pid == $pidd);
}
return 0;
}