| 1 | package XAS::Lib::Modules::Alerts; | ||
| 2 | |||
| 3 | our $VERSION = '0.04'; | ||
| 4 | |||
| 5 | use DateTime; | ||
| 6 | use Try::Tiny; | ||
| 7 | use XAS::Factory; | ||
| 8 | |||
| 9 | use XAS::Class | ||
| 10 | debug => 0, | ||
| 11 | version => $VERSION, | ||
| 12 | base => 'XAS::Singleton', | ||
| 13 | constants => ':alerts', | ||
| 14 | accessors => 'spooler', | ||
| 15 | codec => 'JSON', | ||
| 16 | utils => 'dt2db', | ||
| 17 | filesystem => 'Dir' | ||
| 18 | ; | ||
| 19 | |||
| 20 | # ------------------------------------------------------------------------ | ||
| 21 | # Public Methods | ||
| 22 | # ------------------------------------------------------------------------ | ||
| 23 | |||
| 24 | sub send { | ||
| 25 | my $self = shift; | ||
| 26 | |||
| 27 | my $p = $self->validate_params(\@_, { | ||
| 28 | -message => 1, | ||
| 29 | -process => 1, | ||
| 30 | -facility => { optional => 1, default => 'systems', regex => ALERT_FACILITY }, | ||
| 31 | -priority => { optional => 1, default => 'low', regex => ALERT_PRIORITY }, | ||
| 32 | }); | ||
| 33 | |||
| 34 | my $dt = DateTime->now(time_zone => 'local'); | ||
| 35 | |||
| 36 | my $data = { | ||
| 37 | hostname => $self->env->host, | ||
| 38 | datetime => dt2db($dt), | ||
| 39 | process => $p->{'process'}, | ||
| 40 | pid => $$, | ||
| 41 | msgid => 0, | ||
| 42 | priority => $p->{'priority'}, | ||
| 43 | facility => $p->{'facility'}, | ||
| 44 | message => $p->{'message'}, | ||
| 45 | }; | ||
| 46 | |||
| 47 | my $json = encode($data); | ||
| 48 | |||
| 49 | $self->spooler->write($json); | ||
| 50 | |||
| 51 | } | ||
| 52 | |||
| 53 | # ------------------------------------------------------------------------ | ||
| 54 | # Private Methods | ||
| 55 | # ------------------------------------------------------------------------ | ||
| 56 | |||
| 57 | sub init { | ||
| 58 | my $class = shift; | ||
| 59 | |||
| 60 | my $self = $class->SUPER::init(@_); | ||
| 61 | |||
| 62 | $self->{spooler} = XAS::Factory->module('spooler', { | ||
| 63 | -directory => Dir($self->env->spool, 'alerts'), | ||
| 64 | -mask => 0777 | ||
| 65 | }); | ||
| 66 | |||
| 67 | return $self; | ||
| 68 | |||
| 69 | } | ||
| 70 | |||
| 71 | 1; | ||
| 72 | |||
| 73 | __END__ | ||
| 74 | |||
| 75 | =head1 NAME | ||
| 76 | |||
| 77 | XAS::Lib::Modules::Alerts - The alert module for the XAS environment | ||
| 78 | |||
| 79 | =head1 SYNOPSIS | ||
| 80 | |||
| 81 | Your program can use this module in the following fashion: | ||
| 82 | |||
| 83 | use XAS::Lib::Modules::Alerts; | ||
| 84 | |||
| 85 | my $alert = XAS::Lib::Modules::Alerts->new(); | ||
| 86 | |||
| 87 | $alert->send( | ||
| 88 | -priority => 'high', | ||
| 89 | -facility => 'huston', | ||
| 90 | -message => 'There is a problem' | ||
| 91 | ); | ||
| 92 | |||
| 93 | =head1 DESCRIPTION | ||
| 94 | |||
| 95 | This is the module for sending alerts within the XAS environment. It will write | ||
| 96 | an "alert" to the alerts spool directory. It is implemented as a singleton | ||
| 97 | and will auto-load when invoked. | ||
| 98 | |||
| 99 | =head1 METHODS | ||
| 100 | |||
| 101 | =head2 new | ||
| 102 | |||
| 103 | This method initializes the module. | ||
| 104 | |||
| 105 | =head2 send | ||
| 106 | |||
| 107 | This method will send an alert. It takes the following named parameters: | ||
| 108 | |||
| 109 | =over 4 | ||
| 110 | |||
| 111 | =item B<-priority> | ||
| 112 | |||
| 113 | The notification level, 'high','medium','low'. Default 'low'. | ||
| 114 | |||
| 115 | =item B<-facility> | ||
| 116 | |||
| 117 | The notification facility, 'systems', 'dba', etc. Default 'systems'. | ||
| 118 | |||
| 119 | =item B<-message> | ||
| 120 | |||
| 121 | The message text for the message | ||
| 122 | |||
| 123 | =back | ||
| 124 | |||
| 125 | =head1 SEE ALSO | ||
| 126 | |||
| 127 | =over 4 | ||
| 128 | |||
| 129 | =item L<XAS|XAS> | ||
| 130 | |||
| 131 | =back | ||
| 132 | |||
| 133 | =head1 AUTHOR | ||
| 134 | |||
| 135 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 136 | |||
| 137 | =head1 COPYRIGHT AND LICENSE | ||
| 138 | |||
| 139 | Copyright (C) 2014 Kevin L. Esteb | ||
| 140 | |||
| 141 | This library is free software; you can redistribute it and/or modify | ||
| 142 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 143 | at your option, any later version of Perl 5 you may have available. | ||
| 144 | |||
| 145 | See L<http://dev.perl.org/licenses/> for more information. | ||
| 146 | |||
| 147 | =cut |