-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcronbuild.pl
More file actions
231 lines (208 loc) · 6.9 KB
/
cronbuild.pl
File metadata and controls
231 lines (208 loc) · 6.9 KB
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
#!/usr/bin/perl -w
# Perl built in module
# Accepting flags
use Getopt::Std;
# Make use of UNIX locking mechanism
use Fcntl 'LOCK_EX', 'LOCK_UN';
# create path environment
$ENV{'PATH'}='/bin:/sbin:/usr/bin:/usr/sbin:/usr/opt/networker/bin:/usr/local/bin';
# creating verbose date mechanism
($day, $month, $year, $hour, $min, $sec) = (localtime)[3, 4, 5, 2, 1, 0];
$month += 1;
$year += 1900;
$date = "$day-$month-$year.$hour:$min:$sec";
# flags declare
getopts('hdo:g:r:');
# directories variables declaration
$cronDir = "/app/cron";
#system("cd $cronDir");
chdir($cronDir);
$logDir = "$cronDir/log";
$currentCron = "/var/spool/cron/crontabs/root";
$cronUser = 'root';
$host = `/usr/bin/hostname`;
chomp($host);
$hostCron = "$cronDir/$host.cron";
# The following block runs the program:
# 1. checking the relevant flags
# 2. uses the relevant sub routines
# execute "./cronbuild.pl -h" for understanding flags and parameters behavior
if ($opt_h) {
$opt_h = &Help;
} elsif (($opt_o) && (($opt_r) || ($opt_g))) {
$option = $opt_o;
if ($option !~ /add|del/) {
print "You must enter 'add' or 'del' options.\n";
&Help;
exit 1;
}
if ($opt_r) {
$group = $opt_r;
if (&chkResourceState($group) !~ /ONLINE/) {
if ($group !~ /$host/) {
print "\nThe current resource group $group is not online on this node!!!\n";
print "or\n";
print "You are merging the wrong file.cron on host:$host!!!\n\n";
exit 1;
}
}
} else {
$group = $opt_g;
}
if (&chkFile("$cronDir/$group.cron") == 0) {
&cronBuild($option, $group);
if ($opt_d) {
&runDaemon($group);
}
} else {
print "Cron file for group $group not exist.\n";
exit 2;
}
}
if (($opt_d)&&($opt_g)) {
$group = $opt_g;
$opt_d = &runDaemon($group);
}
# create a temporary cron file
system("/usr/bin/touch $cronDir/crontmp.$$");
# a void routine
# creates a new cronfile
# option - operation flag
# group - rg name
sub cronBuild {
$option = $_[0];
$group = $_[1];
&lockFile();
&appendFile2File($currentCron, "$logDir/$group.$$.$date"); #create history log file
&cronBuildPrepare($option, $group);
&addOthers($group);
&newCronCreate();
system("cat $cronDir/crontmp.$$");
unlink("$cronDir/crontmp.$$");
&unLockFile();
}
# Preparing temporary cron file structure
# creating a file containing desired updated groups
# action - add
# grp - rg name
sub cronBuildPrepare {
$action = $_[0];
$grp = $_[1];
if ("$cronDir/$grp.cron" =~ /$hostCron/) {
&appendFile2File($hostCron, "$cronDir/crontmp.$$");
return;
} else {
&appendFile2File($hostCron, "$cronDir/crontmp.$$");
}
if ($action =~ /add/) {
&appendFile2File("$cronDir/$grp.cron", "$cronDir/crontmp.$$");
}
}
# filter current working resource group from other groups
# grp - rg name
sub addOthers {
$grp = shift;
@command = `/usr/cluster/bin/scha_cluster_get -O ALL_RESOURCEGROUPS`;
for ($i=0;$i<=$#command;$i++) {
if ($command[$i] =~ /\b$grp\b-rg/) {
splice(@command, $i, 1);
}
}
foreach (@command) {
chomp;
print "Resource is: $_\n";
@grps = split(/-/, $_);
next if &chkResourceState($grps[0]) !~ /ONLINE/;
if (chkFile("$cronDir/$grps[0].cron") == 0) {
&appendFile2File("$cronDir/$grps[0].cron", "$cronDir/crontmp.$$");
} else {
warn "Cant append file: $cronDir/$grps[0].cron to template: $cronDir/crontmp.$$.\n";
}
}
}
# Checks resource group status
# return - ON if up, else OFF
sub chkResourceState {
$resource = shift;
$grp = "$resource" . '-rg';
# return `/usr/cluster/bin/scha_resource_get -O RESOURCE_STATE -R $resource -G $grp`;
return `/usr/cluster/bin/scha_resourcegroup_get -O RG_STATE -G $grp`;
}
# create a temporary cron file
sub newCronCreate {
system("su - $cronUser -c \"crontab $cronDir/crontmp.$$\"");
}
# creating appending writing mechanism
# fileRead - file to read from.
# fileWrite - file to append to.
sub appendFile2File {
$fileRead = $_[0];
$fileWrite = $_[1];
open(FILEREAD, "< $fileRead") or die "Cant open $fileRead for read: $!\n";
open(FILEWRITE, ">> $fileWrite") or die "Cant open $fileWrite for appen: $!\n";
while (<FILEREAD>){
print FILEWRITE;
}
close FILEWRITE;
close FILEREAD;
}
# void command
# lock the file "cronbuild.lck" by current running program
sub lockFile {
open(LOCK, ">$cronDir/$group.lck") or die "Cannot lock $group.lck - $!\n";
flock(LOCK, LOCK_EX);
seek (LOCK, 0, 2);
print LOCK $$;
}
# void command
# release the file "cronbuild.lck" by current running program
sub unLockFile {
flock(LOCK, LOCK_UN);
close(LOCK);
}
# read from file and its contents
# file - file to read from.
sub getFileContent {
$file = shift;
open(FILE, "< $file") or die "Cant open $file: $!\n";
while (<FILE>) {
print;
}
}
# check if file exist
# return - standard output status
sub chkFile {
$file = shift;
system("ls $file >/dev/null 2>&1");
return $?;
}
# write into a file and erase its current contents
sub writeFile {
$file = $_[0];
$string = $_[1];
open(FILE, "> $file") or die "Cant open $file: $!\n";
print FILE $string;
close FILE;
}
# print help to standard output
sub Help {
print "\n\tcronbuild.pl ver 1.0 written by Doron Dollev Jun 18 2006\n\n";
print "\tcronbuild.pl [-h] [-o <add|del> -r ResourceName]\n\n";
print "\t-d(notice) Daemon - For cluster use only.\n";
print "\t-g(notice) Resource group - For cluster use only.\n";
print "\t-r Name of resource instance.\n";
print "\t-o The options are 'add' or 'del'.\n";
print "\t-h Print this help screen.\n\n\n";
}
# run daemon by the cluster
sub runDaemon {
$grp = shift;
if (&chkFile("$cronDir/$grp.d") == 0) {
exec("$cronDir/$grp.d");
} else {
print "Daemon file $cronDir/$grp.d not exist.\n";
}
}
#system("cat $cronDir/cronbuild.lck");
# delete cron temporary file
unlink("$cronDir/crontmp.$$");