#!/usr/bin/env perl # # Copyright (c) 2020 Mischa Peters # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # # vmm(4)/vmd(8) VM not running notify script for OpenBSD Amsterdam # 2020/05/03 initial release # 2020/05/07 added: exception variable boot="no" # 2020/05/11 added: use local relay to send out email # use 5.024; use strict; use warnings; use autodie; my $email_template = "email-stopped.txt"; # fuction to parse _deploy.conf and vm*.txt files # all variables are stripped and added to either %vms or %conf sub get_variables { my ($hash_name, @files) = @_; my %hash; my $filename; my $vm_name; my $vm_number; for my $file (@files) { # When hash is 'vms' use the vm_name as key # Otherwise use 'conf' as key if ($hash_name eq "vms") { ($filename = $file) =~ s/.*\///; ($vm_name = $filename) =~ s/\.txt//; ($vm_number = $vm_name) =~ s/^vm//; $hash{$vm_name}{'vm_number'} = $vm_number; } open my $fh, "<", "$file"; while (my $row = <$fh>) { next if ($row =~ /^\s*($|#)/); chomp ($row); (my $key, my $val) = split(/=/, $row, 2); if ($hash_name eq "vms") { ($hash{$vm_name}{$key} .= $val) =~ s/^"+|"+$//g; } else { ($hash{$hash_name}{$key} .= $val) =~ s/^"+|"+$//g; } } close $fh; } return %hash; } # function to notify people sub notify { my %conf = %{$_[0]}; my %vms = %{$_[1]}; my $_etc = $conf{'conf'}{'ETC'}; my $_server = $conf{'conf'}{'SERVER'}; my $_vms = $conf{'conf'}{'VMS'}; my $template = "$_vms/../$email_template"; my @stopped_vms = qx(vmctl show | grep stopped | awk '{print \$9}'); for my $vm_name (sort keys %vms) { my $_name = $vms{$vm_name}{'name'}; my $_email = $vms{$vm_name}{'email'}; my $_hostname = $vms{$vm_name}{'hostname'}; my $_boot = $vms{$vm_name}{'boot'} || "yes"; if ($_boot =~ /no/) { print "NOT NOTIFIED: $_name, $_email, $_hostname, $_server ($vm_name)\n"; next; } if (grep(/$vm_name/, @stopped_vms)) { (my $_firstname, my $_lastname) = split(/ /, $_name, 2); my $_ipaddress = qx(grep -A2 $vm_name $_etc/dhcpd.conf | awk '/fixed-address/{print \$2}' | sed 's/;//' | tr -d '\n'); print "NOTIFIED: $_name, $_email, $_hostname, $_server ($vm_name), $_ipaddress\n"; open(my $fh, '<', $template) or die "Could not open file '$template' $!"; open my $fh_email, "|-", "/usr/sbin/sendmail -t"; printf $fh_email "To: %s\n", $_email; while (my $row = <$fh>) { chomp $row; $row =~ s/NAME/$_firstname/g; $row =~ s/IP$/$_ipaddress/g; $row =~ s/VMID/$vm_name/g; $row =~ s/SERVER/$_server/g; print $fh_email "$row\n"; } close $fh_email; } } } # function to print all keys & values for debug purposes sub debug_parse { my %conf = %{$_[0]}; my %vms = %{$_[1]}; print "All VMs\n##\n"; for my $vm_name (sort keys %vms) { my $_date = $vms{$vm_name}{'date'}; my $_payment = $vms{$vm_name}{'payment'}; my $_donated = $vms{$vm_name}{'donated'}; my $_name = $vms{$vm_name}{'name'}; my $_email = $vms{$vm_name}{'email'}; my $_hostname = $vms{$vm_name}{'hostname'}; print "$_date, $_payment, $_name, $_email, $_hostname, ($vm_name)\n"; } print "##\n"; } # check if _deploy.conf exists my $dev = $ENV{'HOME'} . "/openbsd.amsterdam/deploy.pl"; my $prod = $ENV{'HOME'}; my $dir; my $debug; my %conf; my %vms; if (-d "$dev") { $dir = $dev; $debug = 1; } else { $dir = $prod; } if (-e "$dir/_deploy.conf") { %conf = get_variables('conf', "$dir/_deploy.conf"); } else { printf "Unable to find config file in current directory (%s).\n", $dir; printf "Create the config file _deploy.conf in %s.\n", $dir; exit 1; } # parse all vm*.txt files in the VMS directory my @files = glob "$conf{'conf'}{'VMS'}/*.txt"; %vms = get_variables('vms', @files); # run all functions if ($debug) { debug_parse(\%conf, \%vms); } notify(\%conf, \%vms);