]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/scripts/connscheduler
Fixed conn scheduler restart collectd when morning reconnect (graphs)
[people/pmueller/ipfire-2.x.git] / src / scripts / connscheduler
1 #!/usr/bin/perl
2 #
3 # IPFire Connection Scheduler (F)Cron Job
4 #
5 # This code is distributed under the terms of the GPL
6 # The original code is taken from weizen_42.
7 # See /home/httpd/cgi-bin/connscheduler.cgi
8 #
9
10 use strict;
11
12 require '/var/ipfire/general-functions.pl';
13 require '/var/ipfire/connscheduler/lib.pl';
14
15 # seems to be necessary
16 my $sleep_after_profile = 5;
17
18 my ($second, $minute, $hour, $day, $month ,$year, $weekday) = localtime(time);
19 # correction for weekday, I am used to weeks starting with Monday (= 0) ;-)
20 $weekday = ($weekday + 6) % 7;
21 # get the closest thing possible
22 $minute = int($minute / 5) * 5;
23
24
25 if ( $ARGV[0] eq 'hangup' )
26 {
27 &hangup();
28 }
29 elsif ( $ARGV[0] eq 'dial' )
30 {
31 &dial();
32 }
33 elsif ( $ARGV[0] eq 'reconnect' )
34 {
35 &reconnect();
36 }
37 elsif ( $ARGV[0] eq 'profile' )
38 {
39 &profile($ARGV[1]);
40 }
41 elsif ( $ARGV[0] eq 'timer' )
42 {
43 &timer();
44 }
45 elsif ( $ARGV[0] eq 'test' )
46 {
47 &test();
48 }
49 else
50 {
51 print "Usage: $0 {dial | hangup | reconnect | profile nr# }\n";
52 }
53
54 exit 0;
55
56
57 # __ _ _
58 # / _| | | (_)
59 # | |_ _ _ _ __ ___| |_ _ ___ _ __ ___
60 # | _| | | | '_ \ / __| __| |/ _ \| '_ \/ __|
61 # | | | |_| | | | | (__| |_| | (_) | | | \__ \
62 # |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
63 #
64 sub hangup
65 {
66 # Kill connectd if running to prevent redial
67 system('/bin/killall', 'connectd');
68
69 unless ( -e "${General::swroot}/red/active" )
70 {
71 &General::log("ConnSched already disconnected");
72 return;
73 }
74
75 &General::log("ConnSched disconnect");
76 unless ( system('/etc/rc.d/init.d/network', 'stop', 'red') == 0 )
77 {
78 &General::log("ConnSched disconnect failed: $?");
79 return;
80 }
81
82 # now wait for active triggerfile and ppp daemon to disappear
83 # wait maximum 60 seconds
84 my $counter = 60;
85 sleep 1;
86 while ( -e "${General::swroot}/red/active" || -e '/var/run/ppp-ipfire.pid' || $counter == 0 )
87 {
88 sleep 1;
89 $counter--;
90 }
91 }
92
93
94 sub dial
95 {
96 if ( -e "${General::swroot}/red/active" )
97 {
98 &General::log("ConnSched already connected");
99 return;
100 }
101
102 &General::log("ConnSched connect");
103 unless ( system('/etc/rc.d/init.d/network', 'start', 'red') == 0 )
104 {
105 &General::log("ConnSched connect failed: $?");
106 return;
107 }
108
109 # wait maximum 60 seconds for active triggerfile
110 my $counter = 60;
111 until ( -e "${General::swroot}/red/active" || $counter == 0 )
112 {
113 sleep 1;
114 $counter--;
115 }
116 }
117
118
119 sub reconnect
120 {
121 &hangup() if ( -e "${General::swroot}/red/active" );
122 # now wait for active triggerfile and ppp daemon to disappear
123 # wait maximum 60 seconds
124 my $counter = 60;
125 sleep 1;
126 while ( -e "${General::swroot}/red/active" || -e '/var/run/ppp-ipfire.pid' || $counter == 0 )
127 {
128 sleep 1;
129 $counter--;
130 }
131 /etc/init.d/collectd stop
132 &dial();
133 /etc/init.d/collectd start
134 }
135
136
137 sub profile
138 {
139 my $profile = shift;
140 my $restart_red = 0;
141
142 unless ( ($profile > 0) and ($profile < $CONNSCHED::maxprofiles) )
143 {
144 &General::log("ConnSched invalid profile: $profile");
145 return;
146 }
147
148 unless ( -e "${General::swroot}/ppp/settings-$profile" )
149 {
150 &General::log("ConnSched profile file does not exist: $profile");
151 return;
152 }
153
154 if ( -e "${General::swroot}/red/active" )
155 {
156 # remember to restart red after changing profile
157 $restart_red = 1;
158 &hangup();
159 }
160
161 &General::log("ConnSched select profile: $profile");
162
163 # Method to change Profile from pppsetup.cgi
164 unlink("${General::swroot}/ppp/settings");
165 link("${General::swroot}/ppp/settings-$profile", "${General::swroot}/ppp/settings");
166 system ("/usr/bin/touch", "${General::swroot}/ppp/updatesettings");
167
168 if ( $restart_red == 1 )
169 {
170 ## FIXME: do we need to do this ?
171 sleep($sleep_after_profile);
172 &dial();
173 }
174 }
175
176
177 # fcronjob entry
178 sub timer
179 {
180 for my $i ( 0 .. $#CONNSCHED::config )
181 {
182 next if ( $CONNSCHED::config[$i]{'ACTIVE'} ne 'on' );
183
184 my $action_hour = substr($CONNSCHED::config[$i]{'TIME'},0,2);
185 my $action_minute = substr($CONNSCHED::config[$i]{'TIME'},3,2);
186
187 next if ( $action_hour != $hour );
188 next if ( $action_minute != $minute );
189
190 if ( $CONNSCHED::config[$i]{'DAYSTYPE'} eq 'days' )
191 {
192 my @temp = split(/-/,$CONNSCHED::config[$i]{'DAYS'},2);
193
194 my $daystart = substr($temp[0], 0, -1);
195 my $dayend = substr($temp[1], 1);
196
197 next if ( ($day < $daystart) || ($day > $dayend) );
198 }
199 else
200 {
201 next if ( index($CONNSCHED::config[$i]{'WEEKDAYS'}, $CONNSCHED::weekdays[$weekday]) == -1 );
202 }
203
204
205 if ( $CONNSCHED::config[$i]{'ACTION'} eq 'reconnect' )
206 {
207 &reconnect()
208 }
209 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'dial' )
210 {
211 &dial();
212 }
213 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'hangup' )
214 {
215 &hangup();
216 }
217 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'select profile' )
218 {
219 &profile($CONNSCHED::config[$i]{'PROFILENR'});
220 }
221 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'reboot' )
222 {
223 &General::log("ConnSched reboot");
224 system ("/usr/local/bin/ipfirereboot", "boot");
225 }
226 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'shutdown' )
227 {
228 &General::log("ConnSched shutdown");
229 system ("/usr/local/bin/ipfirereboot", "down");
230 }
231 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'ipsecstart' )
232 {
233 &General::log("ConnSched ipsecstart");
234 system ("/usr/local/bin/ipsecctrl", "S");
235 }
236 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'ipsecstop' )
237 {
238 &General::log("ConnSched ipsecstop");
239 system ("/usr/local/bin/ipsecctrl", "D");
240 }
241 else
242 {
243 # okay ? an event we don't know about
244 }
245 }
246 }