| 1 | #!/usr/bin/perl -w |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | |
|---|
| 5 | use Getopt::Long; |
|---|
| 6 | use Net::SMTP::TLS; |
|---|
| 7 | |
|---|
| 8 | # parse command line options |
|---|
| 9 | my $reboot_flag = '1'; |
|---|
| 10 | my $shutdown_time = '04:00'; |
|---|
| 11 | GetOptions('reboot!' => \$reboot_flag, 'time=s' => \$shutdown_time); |
|---|
| 12 | unless($shutdown_time =~ m/^[012]\d:[012345]\d$/) { |
|---|
| 13 | print "Invalid time: $shutdown_time\n"; |
|---|
| 14 | exit 1; |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | # email server config |
|---|
| 18 | my $smtp_server = 'smtp.gmail.com'; |
|---|
| 19 | my $smtp_user = 'mat.booth@gmail.com'; |
|---|
| 20 | my $smtp_password = 'music_trend'; |
|---|
| 21 | my $smtp_port = 587; |
|---|
| 22 | |
|---|
| 23 | # get list of each vhosts' admin email addresses |
|---|
| 24 | my %sites; |
|---|
| 25 | foreach my $conf (glob "./*/site.conf") { |
|---|
| 26 | open CONF, $conf or die($!); |
|---|
| 27 | $conf =~ s/\.\/(.*)\/site\.conf/$1/; |
|---|
| 28 | while(<CONF>) { |
|---|
| 29 | chomp; |
|---|
| 30 | $sites{$conf} = $_ if s/^ServerAdmin\s+(\S*)/$1/i; |
|---|
| 31 | } |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | foreach my $vhost (keys %sites) { |
|---|
| 35 | |
|---|
| 36 | # email config |
|---|
| 37 | my $email_to = $sites{$vhost}; |
|---|
| 38 | my $email_from = 'admin@matbooth.co.uk'; |
|---|
| 39 | my $email_subject = "$vhost is going down for maintenance!"; |
|---|
| 40 | my $email_replyto = 'admin@matbooth.co.uk'; |
|---|
| 41 | |
|---|
| 42 | # build smtp headers |
|---|
| 43 | my @head; |
|---|
| 44 | push(@head, "To: $email_to\n"); |
|---|
| 45 | push(@head, "From: $email_from\n"); |
|---|
| 46 | push(@head, "Subject: $email_subject\n"); |
|---|
| 47 | push(@head, "Reply-to: $email_replyto\n"); |
|---|
| 48 | push(@head, "Content-Type: text/plain; charset=UTF-8\n"); |
|---|
| 49 | push(@head, "Content-Transfer-Encoding: 8bit\n"); |
|---|
| 50 | push(@head, "\n"); |
|---|
| 51 | |
|---|
| 52 | # build message body |
|---|
| 53 | my @body; |
|---|
| 54 | push(@body, "Dear Webmaster,\n\n"); |
|---|
| 55 | push(@body, "Recent security related kernel updates require that we reboot the server your website is hosted on. All services at $vhost will be unavailable from $shutdown_time for approximately 5-10 minutes. If this is inconvenient, please email me and I will reschedule the reboot for another time.\n\n"); |
|---|
| 56 | push(@body, "Peace out,\nMat\n\n"); |
|---|
| 57 | push(@body, "------------------------------\n\n"); |
|---|
| 58 | push(@body, "This is an automatically generated message. You are receiving it because the ServerAdmin directive in the Apache virtual host config for $vhost is set to this email address. If you do not believe this message was intended for you, please email me immediately so that I can correct the mistake.\n"); |
|---|
| 59 | push(@body, "\n"); |
|---|
| 60 | |
|---|
| 61 | # send email |
|---|
| 62 | my $smtp = new Net::SMTP::TLS($smtp_server, Port => $smtp_port, User => $smtp_user, Password => $smtp_password); |
|---|
| 63 | $smtp->mail($email_from); |
|---|
| 64 | $smtp->to($email_to); |
|---|
| 65 | $smtp->data(); |
|---|
| 66 | $smtp->datasend(@head, @body); |
|---|
| 67 | $smtp->dataend(); |
|---|
| 68 | $smtp->quit(); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | # issue shutdown command |
|---|
| 72 | if($reboot_flag eq "1") { |
|---|
| 73 | system "shutdown -r $shutdown_time &"; |
|---|
| 74 | } else { |
|---|
| 75 | system "shutdown -h $shutdown_time &"; |
|---|
| 76 | } |
|---|
| 77 | exit 0; |
|---|