-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.pl
executable file
·159 lines (140 loc) · 4.08 KB
/
start.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
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
#!/usr/bin/perl
####################################
# Argos Startscript #
# SURFids 2.10 #
# Changeset 001 #
# 19-11-2008 #
# Jan van Lith & Kees Trippelvitz #
####################################
#############################################
# Changelog:
# 002 DB connect improved
# 001 Initial release
#############################################
use DBI;
use Time::localtime;
do '/opt/argos/argos.conf';
# Stop all processes
stop();
# Opening log file
open(LOG, ">> $logfile");
while(1) {
$dbh = DBI->connect($dsn, $pgsql_user, $pgsql_pass)
or die $DBI::errstr;
@pids = `ls $piddir |grep argos_`;
$numimage_run = @pids;
$sql = "SELECT DISTINCT(argos_images.imagename), argos_images.osname, argos_images.oslang FROM argos_images, argos WHERE argos.imageid = argos_images.id AND argos_images.serverip = '$listenip'";
$query = $dbh->prepare($sql);
$numimage_db = $query->execute();
if ($numimage_run == $numimage_db) {
### Check if image has to be changed
$ts = getts();
while (@row = $query->fetchrow_array) {
$imagename = $row[0];
$imagename =~ s/\.img/\.pid/;
$image = $row[0];
$osname = $row[1];
$oslang = $row[2];
$pidfileargosstart = "$piddir/argosstart_$imagename";
if (! -e "$pidfileargosstart") {
$ts = getts();
print LOG "[$ts] Starting new image: $image\n";
system("$homedir/argosstart.pl $image $osname $oslang &");
}
}
}
elsif ($numimage_run > $numimage_db) {
### Running argos image(s) has to be killed
$ts = getts();
foreach $pidfileargos (@pids) {
$image = $pidfileargos;
chomp($image);
$pidfileargos = "$piddir/$pidfileargos";
$image =~ s/argos_//;
$image =~ s/\.pid/\.img/;
$sql_image = "SELECT argos_images.imagename FROM argos_images, argos WHERE argos.imageid = argos_images.id AND argos_images.imagename = '$image'";
$query_image = $dbh->prepare($sql_image);
$numimage = $query_image->execute();
$imagename = $image;
$imagename =~ s/\.img/\.pid/;
if ($numimage == 0) {
$ts = getts();
$pidfileargosstart = "$piddir/argosstart_$imagename";
$pidargosstart = `cat $pidfileargosstart`;
$pidargos = `cat $pidfileargos`;
chomp($pidargos);
chomp($pidargosstart);
`kill $pidargosstart`;
`kill $pidargos`;
`rm $pidfileargosstart`;
print LOG "[$ts] Killing argos image: $image with pid: $pidargos & $pidargosstart (rm $pidfileargosstart)\n";
}
}
}
elsif ($numimage_db > $numimage_run) {
### Argos image(s) has to be started
$ts = getts();
while (@row = $query->fetchrow_array) {
$imagename = $row[0];
$imagename =~ s/\.img/\.pid/;
$image = $row[0];
$osname = $row[1];
$oslang = $row[2];
$pidfileargosstart = "$piddir/argosstart_$imagename";
if (! -e "$pidfileargosstart") {
$ts = getts();
print LOG "[$ts] Starting new image: $image\n";
system("$homedir/argosstart.pl $image $osname $oslang &");
}
}
}
if (! -e "$piddir/argosalertdetail.pid") {
$ts = getts();
print LOG "[$ts] Starting Argos Alert Detail Listener\n";
system("$homedir/argosalertdetail.pl &");
}
sleep $delay;
$query->finish();
$dbh->disconnect();
}
close(LOG);
sub getts() {
my ($ts, $year, $month, $day, $hour, $min, $sec, $timestamp);
$ts = time();
$year = localtime->year() + 1900;
$month = localtime->mon() + 1;
if ($month < 10) {
$month = "0" . $month;
}
$day = localtime->mday();
if ($day < 10) {
$day = "0" . $day;
}
$hour = localtime->hour();
if ($hour < 10) {
$hour = "0" . $hour;
}
$min = localtime->min();
if ($min < 10) {
$min = "0" . $min;
}
$sec = localtime->sec();
if ($sec < 10) {
$sec = "0" . $sec;
}
$timestampdir = "$day$month$year";
$timestamp = "$day-$month-$year $hour:$min:$sec";
}
sub stop() {
@pids = `ps -ef |grep argos | grep -v grep | awk '{print \$2}' | grep -v $$`;
foreach $pid (@pids) {
chomp($pid);
`kill -9 $pid`;
print "Killing pid: $pid\n";
}
$pidscount = `ls -l $piddir |grep \.pid| wc -l`;
if ($pidscount > 0) {
`rm $piddir/*`;
print "Removing pids in $piddir\n";
}
}