| 1 | package Service::Engine::Alerting; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp; | ||
| 7 | use Service::Engine; | ||
| 8 | use Data::Dumper; | ||
| 9 | use Module::Runtime qw(require_module); | ||
| 10 | |||
| 11 | our $Config; | ||
| 12 | our $Log; | ||
| 13 | our $AlertingConfig = {}; | ||
| 14 | our $Modules = {}; | ||
| 15 | our $EngineName; | ||
| 16 | our $Contacts = {}; | ||
| 17 | our $Groups = {}; | ||
| 18 | |||
| 19 | # load desired classes | ||
| 20 | # Service::Engine::Alerting:* | ||
| 21 | |||
| 22 | sub new { | ||
| 23 | |||
| 24 | my ($class,$options) = @_; | ||
| 25 | |||
| 26 | # set some defaults | ||
| 27 | my $attributes = {'modules'=>{}}; | ||
| 28 | |||
| 29 | # load options | ||
| 30 | if (ref($options) eq 'HASH') { | ||
| 31 | foreach my $option (keys %{$options}) { | ||
| 32 | $attributes->{$option} = $options->{$option}; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | # pull in some Service::Engine globals | ||
| 37 | $Config = $Service::Engine::Config; | ||
| 38 | $Log = $Service::Engine::Log; | ||
| 39 | $EngineName = $Service::Engine::EngineName; | ||
| 40 | |||
| 41 | # create our alerting handles | ||
| 42 | #$Modules->{$handle}->$module_method | ||
| 43 | $Log->log({'msg'=>"loading Alerting",'level'=>1}); | ||
| 44 | |||
| 45 | if (ref($Config->get_config('alerting')) eq 'HASH') { | ||
| 46 | $AlertingConfig = $Config->get_config('alerting'); | ||
| 47 | |||
| 48 | if ($AlertingConfig->{'enabled'}) { | ||
| 49 | $Log->log({'msg'=>"Alerting enabled",'level'=>2}); | ||
| 50 | foreach my $service (keys $AlertingConfig->{'modules'}) { | ||
| 51 | my $module_config = $AlertingConfig->{'modules'}->{$service}; | ||
| 52 | if (ref($module_config) eq 'HASH' && keys %{$module_config}) { | ||
| 53 | foreach my $handle (keys %{$module_config}) { | ||
| 54 | next unless keys %{$module_config->{$handle}}; | ||
| 55 | next unless $module_config->{$handle}->{'enabled'}; | ||
| 56 | $Log->log({'msg'=>"loading $handle for $service",'level'=>2}); | ||
| 57 | my $package = 'Service::Engine::Alerting::' . ucfirst lc $service; | ||
| 58 | if ($module_config->{$handle}->{'module_name'}) { | ||
| 59 | $package = $EngineName . '::Modules::' . $module_config->{$handle}->{'module_name'}; | ||
| 60 | } | ||
| 61 | my $res = eval { require_module($package) }; | ||
| 62 | if ($@) { | ||
| 63 | $Log->log({'msg'=>"error loading $handle for $service: $@",'level'=>1}); | ||
| 64 | } else { | ||
| 65 | my $obj = $package->new($module_config->{$handle}); # passes in the config | ||
| 66 | $Modules->{$handle} = $attributes->{'modules'}->{$handle} = $obj; | ||
| 67 | } | ||
| 68 | |||
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | my $contact_config = $Config->get_config('contacts'); | ||
| 77 | if (ref($contact_config) eq 'HASH'){ | ||
| 78 | $Contacts = $contact_config; | ||
| 79 | } else { | ||
| 80 | $Log->log({'msg'=>"error loading contacts",'level'=>2}); | ||
| 81 | } | ||
| 82 | |||
| 83 | my $groups_config = $Config->get_config('groups'); | ||
| 84 | if (ref($groups_config) eq 'HASH'){ | ||
| 85 | $Groups = $groups_config; | ||
| 86 | } else { | ||
| 87 | $Log->log({'msg'=>"error loading groups",'level'=>2}); | ||
| 88 | } | ||
| 89 | |||
| 90 | my $self = bless $attributes, $class; | ||
| 91 | |||
| 92 | return $self; | ||
| 93 | |||
| 94 | } | ||
| 95 | |||
| 96 | sub send { | ||
| 97 | |||
| 98 | my ($self,$args) = @_; | ||
| 99 | |||
| 100 | return 0 unless $AlertingConfig->{'enabled'}; | ||
| 101 | |||
| 102 | my $module = $args->{'module'}; | ||
| 103 | my $handle = $args->{'handle'}; | ||
| 104 | my $msg = $args->{'msg'}; | ||
| 105 | my $options = $args->{'options'}; | ||
| 106 | my $module_method = $AlertingConfig->{'modules'}->{$module}->{$handle}->{'method'}; | ||
| 107 | |||
| 108 | if (ref($options) ne 'HASH') { | ||
| 109 | $Log->log({'msg'=>"error loading options",'level'=>2}); | ||
| 110 | return 0; | ||
| 111 | } | ||
| 112 | |||
| 113 | if (!$module_method) { | ||
| 114 | $Log->log({'msg'=>"error: missing module method",'level'=>2}); | ||
| 115 | return 0; | ||
| 116 | } | ||
| 117 | |||
| 118 | $Log->log({'msg'=>"sending a $module:$module_method alert",'level'=>3}); | ||
| 119 | |||
| 120 | if (!$Modules->{$handle}) { | ||
| 121 | $Log->log({'msg'=>"error: missing $module:$handle",'level'=>2}); | ||
| 122 | return 0; | ||
| 123 | } | ||
| 124 | |||
| 125 | # send out an alert | ||
| 126 | $Modules->{$handle}->$module_method({'msg'=>$msg, 'handle'=>$handle, 'options'=>$options}); | ||
| 127 | |||
| 128 | return 1; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub contacts { | ||
| 132 | my ($self) = @_; | ||
| 133 | return $Contacts; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub groups { | ||
| 137 | my ($self) = @_; | ||
| 138 | return $Groups; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub get_recipients { | ||
| 142 | my ($self,$args) = @_; | ||
| 143 | |||
| 144 | my $contacts = {}; | ||
| 145 | my $contact_names = $args->{'contacts'}; | ||
| 146 | if (ref($contact_names) eq 'ARRAY') { | ||
| 147 | foreach my $contact_name (@{$contact_names}) { | ||
| 148 | |||
| 149 | my $contact = $Contacts->{$contact_name}; | ||
| 150 | if (ref($contact) eq 'HASH') { | ||
| 151 | $contacts->{$contact_name} = $contact; | ||
| 152 | } else { | ||
| 153 | $Log->log({'msg'=>"$contact_name not found",'level'=>2}); | ||
| 154 | } | ||
| 155 | |||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | my $group_members = $self->get_group_contacts($args->{'groups'}); | ||
| 160 | |||
| 161 | my %merged = (%$contacts,%$group_members); | ||
| 162 | $contacts = \%merged; | ||
| 163 | |||
| 164 | return $contacts; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub get_group_contacts { | ||
| 168 | |||
| 169 | my ($self,$groups) = @_; | ||
| 170 | |||
| 171 | my $contacts = {}; | ||
| 172 | |||
| 173 | if (ref($groups) eq 'ARRAY') { | ||
| 174 | foreach my $group (@{$groups}) { | ||
| 175 | # get the group members | ||
| 176 | my $group_members = $Groups->{$group}; | ||
| 177 | if (ref($group_members) eq 'ARRAY') { | ||
| 178 | foreach my $contact_name (@{$group_members}) { | ||
| 179 | my $contact = $Contacts->{$contact_name}; | ||
| 180 | if (ref($contact) eq 'HASH') { | ||
| 181 | $contacts->{$contact_name} = $contact; | ||
| 182 | } else { | ||
| 183 | $Log->log({'msg'=>"$contact_name not found",'level'=>2}); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | } | ||
| 191 | |||
| 192 | return $contacts; | ||
| 193 | } | ||
| 194 | |||
| 195 | 1; |