source: xas-logmon/trunk/lib/XAS/Apps/Logmon/Monitor.pm @ e241e2a12f7135a86114491b06c61bfce547f401

Revision e241e2a12f7135a86114491b06c61bfce547f401, 6.3 KB checked in by Kevin L. Esteb <kevin@…>, 4 years ago (diff)

updating documentation

  • Property mode set to 100644
Line 
1package XAS::Apps::Logmon::Monitor;
2
3our $VERSION = '0.01';
4
5use XAS::Lib::Process;
6
7use XAS::Class
8  debug      => 0,
9  version    => $VERSION,
10  base       => 'XAS::Lib::App::Service',
11  mixin      => 'XAS::Lib::Mixins::Configs',
12  utils      => 'dotid trim :env',
13  constants  => 'TRUE FALSE',
14  accessors  => 'cfg',
15  filesystem => 'File Dir',
16  vars => {
17    SERVICE_NAME         => 'XAS_Log',
18    SERVICE_DISPLAY_NAME => 'XAS Log Monitor',
19    SERVICE_DESCRIPTION  => 'XAS log file monitor'
20  }
21;
22
23# ----------------------------------------------------------------------
24# Public Methods
25# ----------------------------------------------------------------------
26
27sub setup {
28    my $self = shift;
29
30    my @sections = $self->cfg->Sections();
31
32    foreach my $section (@sections) {
33
34        next if ($section !~ /^logmon:/);
35
36        my $env      = {};
37        my ($alias)  = $section =~ /^logmon:(.*)/;
38        my $ignore   = $self->cfg->val($section, 'ignore', '30');
39        my $filename = File($self->cfg->val($section, 'filename', '/var/logs/xas/xas-spooler.log'));
40        my $spooldir = Dir($self->cfg->val($section, 'spooldir', '/var/spool/xas/logs'));
41        my $cmd      = File($self->cfg->val($section, 'command'));
42
43        my $command = sprintf('%s --filename %s --spooldir %s --ignore %s --process %s --log-type console',
44            $cmd,
45            $filename,
46            $spooldir,
47            $ignore,
48            $alias
49        );
50
51        $alias = trim($alias);
52
53        if (my $e = $self->cfg->val($section, 'environment', undef)) {
54
55            $env = env_parse($e);
56
57        }
58
59        my $process = XAS::Lib::Process->new(
60            -alias          => $alias,
61            -pty            => 1,
62            -command        => $command,
63            -auto_start     => 1,
64            -auto_restart   => 1,
65            -directory      => Dir($self->cfg->val($section, 'directory', "/")),
66            -environment    => $env,
67            -exit_codes     => $self->cfg->val($section, 'exit-codes', '0,1'),
68            -exit_retries   => $self->cfg->val($section, 'exit-retires', -1),
69            -group          => $self->cfg->val($section, 'group', 'xas'),
70            -priority       => $self->cfg->val($section, 'priority', '0'),
71            -umask          => $self->cfg->val($section, 'umask', '0022'),
72            -user           => $self->cfg->val($section, 'user', 'xas'),
73            -redirect       => 1,
74            -output_handler => sub {
75                my $output = shift;
76                $output = trim($output);
77                if (my ($level, $line) = $output =~/\s+(\w+)\s+-\s+(.*)/ ) {
78                    $level = lc(trim($level));
79                    $line  = trim($line);
80                    $self->log->$level(sprintf('%s: %s', $alias, $line));
81                } else {
82                    $self->log->info(sprintf('%s: -> %s', $alias, $output));
83                }
84            }
85        );
86
87        $self->service->register($alias);
88
89    }
90
91}
92
93sub main {
94    my $self = shift;
95
96    $self->log->info_msg('startup');
97
98    $self->setup();
99    $self->service->run();
100
101    $self->log->info_msg('shutdown');
102
103}
104
105# ----------------------------------------------------------------------
106# Private Methods
107# ----------------------------------------------------------------------
108
109sub init {
110    my $class = shift;
111
112    my $self = $class->SUPER::init(@_);
113
114    $self->load_config();
115
116    return $self;
117
118}
119
1201;
121
122__END__
123
124=head1 NAME
125
126XAS::Apps::Logmon::Monitor - A class for the XAS environment
127
128=head1 SYNOPSIS
129
130 use XAS::Apps::Logmon::Monitor;
131
132 my $app = XAS::Apps::Logmon::Monitor->new(
133     -throws => 'xas-logmon'
134 );
135
136 exit $app->run;
137
138=head1 DESCRIPTION
139
140This module will spawn multiple log monitoring processes. It will keep track
141of them and restart them if they should die. Any output from the monitoring
142processes are written into the log file.
143
144=head1 CONFIGURATION
145
146The configuration file is the familiar Windows .ini format. It has the
147following stanzas.
148
149 [logmon: xas-spooler]
150 command = /usr/sbin/xas-logs
151 filename = /var/log/xas/xas-spooler.log
152
153This stanza defines a log file to monitor. There can be multiple stanzas. The
154"xas-spooler" in the stanzas name must be unique. Reasonable defaults
155have been defined for most of the properties. You really only need to use
156'filename' to start a monitoring process.
157
158The following properties may be used.
159
160=over 4
161
162=item B<command>
163
164This is the command to run. Defaults to /usr/sbin/xas-logs.
165
166=item B<filename>
167
168The file to monitor. Defaults to /var/log/xas/xas-spooler.log.
169
170=item B<spooldir>
171
172The spool directory to write messages. Defaults to /var/spool/xas/logs.
173
174=item B<ignore>
175
176The number of days prior to today to ignore. Defaults to 30.
177
178=back
179
180Please see L<XAS::Lib::Process|XAS::Lib::Process> for more details on the
181following parameters.
182
183=over 4
184
185=item B<directory>
186
187The default directory to set for the process. Defaults to "/".
188
189=item B<environment>
190
191Optional additional environment variables to pass to the process.
192
193=item B<exit-codes>
194
195The possible exit codes that might be returned if the process aborts. These
196are used to determine if the process should be restarted. Defaults to "0,1".
197This must be a comma delimited list of values.
198
199=item B<exit-retires>
200
201The number of retries for restarting the process. Defaults to "5". If this is
202"-1" then retries are unlimited. Use with caution.
203
204=item B<group>
205
206The group to run the process under. Defaults to "xas". Not implemented under
207Windows.
208
209=item B<priority>
210
211The priority to run the process under. Defaults to "0". Not implemented under
212Windows.
213
214=item B<umask>
215
216The umask to use for the process. Defaults to "0022". Not implemented under
217Windows.
218
219=item B<user>
220
221The user to run the process under. Defaults to "xas". Not implemented under
222Windows.
223
224=back
225
226=head1 METHODS
227
228=head2 setup
229
230This method will process the config file and spawn the log monitoring
231processes.
232
233=head2 main
234
235This method will start the processing.
236
237=head2 options
238
239No additional command line options are defined.
240
241=head1 SEE ALSO
242
243=over 4
244
245
246=item L<XAS::Logmon|XAS::Logmon>
247
248=item L<XAS|XAS>
249
250=back
251
252=head1 AUTHOR
253
254Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
255
256=head1 COPYRIGHT AND LICENSE
257
258Copyright (c) 2012-2015 Kevin L. Esteb
259
260This is free software; you can redistribute it and/or modify it under
261the terms of the Artistic License 2.0. For details, see the full text
262of the license at http://www.perlfoundation.org/artistic_license_2_0.
263
264=cut
Note: See TracBrowser for help on using the repository browser.