FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Gentoo > Gentoo User

 
 
LinkBack Thread Tools
 
Old 02-26-2010, 05:40 PM
Harry Putnam
 
Default attach a perl script to daemon services

ALERT [ This is a slightly rewritten repost from
`gmane.[...].perl.beginners', where it got no responses]
------- --------- ---=--- --------- --------

My subject line is probably not really that
good at describing what I want advice on but here it is:

I've setup one linux (gentoo) box as a logserver running rsyslog and
several other linux boxes sending syslog info to it.

I want to set the server rsyslog.conf so that in addition to normal
logging to /var/log/whateverlogs it also writes everything to a named
pipe (fifo).

Then tap into the fifo with a perl script that is written to be able
to sort and write the syslog output according to various regex that
may be part of startup cmd or fed in later during the running script.

I think I can manage the sorting/writing and maybe reading regex into
the running process. (Just by an awkward funky method of having the
script reread a file on disk every 5 minutes, and put what ever regex
become necessary there).

There are probably cleaner better ways... and I may be back asking
about that but right now, where I have no real idea is how to attach
the perl script to a service like the system logger.

I mean so the script starts and stops with system logger and somehow
alerts the system logger (and sysadmin) if it (the script) for some
reason is killed or dies on its own while the system logger is still
running.

I imagine getting the perl script to start/stop with the logger daemon
on boot up would be simple enough by inserting something into the init
script that starts the logger.

From there I'm just drawing blanks as to how a script would know if
the system logger stopped, and further some kind of `trap' that would
send out info to be logged by the logger and to the system admin if
the script itself died or was killed.

I'm slightly familiar with shell script traps but haven't encountered
or had need of such in a perl script.

But really I suspect I'm going at this wrong right from the start and
there is a better plan to be had.

I'm hoping someone with that kind of experience can outline how this
might be done. I can handle most of the perl scripting but not sure
about coding a `trap' style function.. That I can get from the
perl.beginners group. so here I'm looking for an overview of how to go
at this.

I'm thinking an experienced sysadmin here will have at the top of
there head.. the basic outline of how something like this might be
done.

So to summarize I want to:
Attach a perl script to a service daemon (rsyslog) so it
starts/stops with the daemon, and has some code that will put
something into syslog output and notify the admin in the event the
script itself is terminated unexpectedly. Something along the line
of a bash/ksh exit `trap'

It would also need to check when being started, that the system
logger is running else fail and send notice to admin.
 
Old 02-26-2010, 07:22 PM
Alan McKinnon
 
Default attach a perl script to daemon services

On Friday 26 February 2010 20:40:40 Harry Putnam wrote:
> ALERT [ This is a slightly rewritten repost from
> `gmane.[...].perl.beginners', where it got no responses]
> ------- --------- ---=--- --------- --------
>
> My subject line is probably not really that
> good at describing what I want advice on but here it is:
>
> I've setup one linux (gentoo) box as a logserver running rsyslog and
> several other linux boxes sending syslog info to it.
>
> I want to set the server rsyslog.conf so that in addition to normal
> logging to /var/log/whateverlogs it also writes everything to a named
> pipe (fifo).
>
> Then tap into the fifo with a perl script that is written to be able
> to sort and write the syslog output according to various regex that
> may be part of startup cmd or fed in later during the running script.

I don't know rsyslog at all (I use syslog-ng), but certain concepts are stable
and universal.

Any log daemon must receive logs from somewhere and send them somewhere, and
almost all support the notion of sending logs to a process instead of a file-
like destination.

Does rsyslog not support something like this:

destination "| /path/to/script.pl"


--
alan dot mckinnon at gmail dot com
 
Old 02-26-2010, 08:44 PM
Harry Putnam
 
Default attach a perl script to daemon services

Alan McKinnon <alan.mckinnon@gmail.com> writes:

>> Then tap into the fifo with a perl script that is written to be able
>> to sort and write the syslog output according to various regex that
>> may be part of startup cmd or fed in later during the running script.
>
> I don't know rsyslog at all (I use syslog-ng), but certain concepts
> are stable and universal.

Please reread my OP or maybe I should attempt to clarify what must be
a poorly written question.

I'm not asking help on anything about writing to a named pipe or
anything about the functioning of rsyslog... I know that part.

Not asking about the bulk of the perl scripting, such as sorting by
regex, reading from fifo, etc (are all understood [at least well enough]).

I want an overview/outline of how one goes about attaching a script to
the operations such as start/stop of a daemon

Even there... the additions necessary to the scripts in
/etc/init.d... are understood.

I guess I want to know if its even advisable to attach custom
scripting to a daemon or if there is a well worn path for doing that.

Assuming its not clear off the wall then:

Where I'm weak is the part where the custom script checks if the
daemon is running, before the script itself starts. That part would
need to be something pretty fool proof... maybe just grep ps output
for the daemon?

And where the custom script sends syslog (and sysadmin) a message in
the event the script itself is killed or dies for some unexpected
reason. Something like an EXIT `trap' in shell scripting... (how its
done is perl)
 
Old 02-26-2010, 10:00 PM
Alan McKinnon
 
Default attach a perl script to daemon services

On Friday 26 February 2010 23:44:03 Harry Putnam wrote:
> Where I'm weak is the part where the custom script checks if the
> daemon is running, before the script itself starts. That part would
> need to be something pretty fool proof... maybe just grep ps output
> for the daemon?

Maybe I'm misunderstanding you, but I don't understand this approach.

If the daemon is not running there's nothing in the fifo. Why got to all the
complexity of writing an independent program that checks if something is
running then does various actions? The script necessarily depends on the
daemon running so instead of checking if it is, simply have the daemon start
the script. No daemon running = no script launched = no complexity.

The script will simply accept what is it is given, no complex checks needed.
Seeing as the input is from a logger, chances are you are inserting logs to a
db, or launching a log analyser, right? Modern loggers support launching child
scripts as a matter of routine.

If I'm still off-bat and this approach cannot work for solid technical
reasons, then I'll go away and shut if if you say so :-)

--
alan dot mckinnon at gmail dot com
 
Old 02-26-2010, 10:34 PM
Michael Higgins
 
Default attach a perl script to daemon services

On Sat, 27 Feb 2010 01:00:43 +0200
Alan McKinnon <alan.mckinnon@gmail.com> wrote:

> > Where I'm weak is the part where the custom script checks if the
> > daemon is running, before the script itself starts. That part would
> > need to be something pretty fool proof... maybe just grep ps output
> > for the daemon?

I have a perl fastAGI script that launches in /etc/init.d/.... I just
cribbed some code from something there...

in my perl script:

log_file=>'Sys::Syslog', pid_file=>'/var/run/evolone_agi.pid';

Sys::Syslog" is an interface to the UNIX syslog(3) program.

from /etc/init.d/evolone_agi

depend() {
need net asterisk postgresql-8.4
}

As for reading new information, there are a gazillion ways. Trap a
signal to reread the configs?

reload() {
ebegin "Reloading evolone_agi configuration"
start-stop-daemon --signal 1 --pidfile /var/run/evolone_agi.pid
eend $? "Error reloading evolone_agi"
}

.. for example.

Hope this helps. '-)


--
| /| | | ~ ~
| / | |---| `|` ?
| |ichael | |iggins ^ /
michael.higgins[at]evolone[dot]org
 
Old 02-27-2010, 05:40 PM
Harry Putnam
 
Default attach a perl script to daemon services

Alan McKinnon <alan.mckinnon@gmail.com> writes:

> On Friday 26 February 2010 23:44:03 Harry Putnam wrote:
>> Where I'm weak is the part where the custom script checks if the
>> daemon is running, before the script itself starts. That part would
>> need to be something pretty fool proof... maybe just grep ps output
>> for the daemon?
>
> Maybe I'm misunderstanding you, but I don't understand this approach.
>
> If the daemon is not running there's nothing in the fifo. Why got to all the
> complexity of writing an independent program that checks if something is
> running then does various actions? The script necessarily depends on the
> daemon running so instead of checking if it is, simply have the daemon start
> the script. No daemon running = no script launched = no complexity.

In the back of my mind there was a reason on opensolaris, that the
script would fail if the fifo was empty... Then once data comes the
script isn't listening. Or syslog won't write or something similar.

I also have an opensolaris box that will be using this same script.

I don't want to back up and relocate all that right now... Not sure I
have it remembered right either, it seems just easier to have the
script check before trying to start (for portability).

Solaris syslog can write to named pipes but no so readily as linux
syslog. Not sure of the details now.

> If I'm still off-bat and this approach cannot work for solid technical
> reasons, then I'll go away and shut if if you say so :-)

No, not off-base... thanks. The checking isn't so hard to figure
anyway, but you didn't talk about the script having some kind of trap
or something to cover unexpected kill or failure.

Michael has covered that somewhat.

Also I didn't think to search cpan for something that does what I
want. That may turn up something I can just plug into.

Michael Higgins <linux@evolone.org> writes:

[...]

> in my perl script:
>
> log_file=>'Sys::Syslog', pid_file=>'/var/run/evolone_agi.pid';
>
> Sys::Syslog" is an interface to the UNIX syslog(3) program.
>
> from /etc/init.d/evolone_agi
>
> depend() {
> need net asterisk postgresql-8.4
> }
>
> As for reading new information, there are a gazillion ways. Trap a
> signal to reread the configs?
>
> reload() {
> ebegin "Reloading evolone_agi configuration"
> start-stop-daemon --signal 1 --pidfile /var/run/evolone_agi.pid
> eend $? "Error reloading evolone_agi"
> }

Thanks ... good info. How does the script behave regarding what
happens if it receives a sigINT or sigKILL, or some other way dies
(even from an internal error)?

Does it log to syslog? and notify sysadmin?

I haven't had occasion to run into an `EXIT trap' in perl... but there
must be such a thing.
 
Old 02-27-2010, 07:57 PM
Michael Higgins
 
Default attach a perl script to daemon services

On Sat, 27 Feb 2010 12:40:17 -0600
Harry Putnam <reader@newsguy.com> wrote:

> > reload() {
> > ebegin "Reloading evolone_agi configuration"
> > start-stop-daemon --signal 1
> > --pidfile /var/run/evolone_agi.pid eend $? "Error reloading
> > evolone_agi" }

> Thanks ... good info. How does the script behave regarding what
> happens if it receives a sigINT or sigKILL, or some other way dies
> (even from an internal error)?

This script is more proof-of-concept piece hacked together in an hour
which just routes my outgoing calls to local free PSTN or SIP. So I know
if it needs restarting when I can't make a call from home. ;-)

Basically, I think if you perldoc Net::Server[::SIG]

In there is:

$self->pre_server_close_hook;

... might be your friend to share some Sys::Syslog routines.

In the Net::Server code:
### set some sigs
$SIG{INT} = $SIG{TERM} = $SIG{QUIT} = sub { $self->server_close; };

### most cases, a closed pipe will take care of itself
$SIG{PIPE} = 'IGNORE';

### catch children (mainly for Fork and PreFork but works for any
chld) $SIG{CHLD} = &sig_chld;

### catch sighup
$SIG{HUP} = sub { $self->sig_hup; }


... seems to have it covered for most cases. ;-) You can see sig_hup is
almost expected to do something.

>
> Does it log to syslog? and notify sysadmin?

I think a good use of Net::Server::* and Sys::Syslog can get you where
you want. I'd bet you can put anything in that pre_server_close_hook
hook... send an email, set off the building alarms... it's perl, after
all. But I really haven't done much directly with it.

>
> I haven't had occasion to run into an `EXIT trap' in perl... but there
> must be such a thing.
>

perldoc -q signal
perldoc perlipc

HTH.

--
| /| | | ~ ~
| / | |---| `|` ?
| |ichael | |iggins ^ /
michael.higgins[at]evolone[dot]org
 
Old 02-28-2010, 08:50 PM
Alan McKinnon
 
Default attach a perl script to daemon services

On Saturday 27 February 2010 20:40:17 Harry Putnam wrote:
> In the back of my mind there was a reason on opensolaris, that the
> script would fail if the fifo was empty... Then once data comes the
> script isn't listening. Or syslog won't write or something similar.
>
> I also have an opensolaris box that will be using this same script.
>
> I don't want to back up and relocate all that right now... Not sure I
> have it remembered right either, it seems just easier to have the
> script check before trying to start (for portability).
>
> Solaris syslog can write to named pipes but no so readily as linux
> syslog. Not sure of the details now.

The rest of your post seems adequately answered elsewhere.

FWIW, Solaris syslogd is like other basic tools on Solaris: standards
compliant in that it caters for the lowest common denominator that comprises
Unix. Which is to say, almost always useless for real work.

I tossed syslogd on Solaris long long ago and migrated everything to syslog-
ng. The nice thing about syslog-ng is that it actually *works*, and does so
predictably.

--
alan dot mckinnon at gmail dot com
 
Old 03-01-2010, 03:16 AM
Harry Putnam
 
Default attach a perl script to daemon services

Alan McKinnon <alan.mckinnon@gmail.com> writes:

> FWIW, Solaris syslogd is like other basic tools on Solaris: standards
> compliant in that it caters for the lowest common denominator that comprises
> Unix. Which is to say, almost always useless for real work.

A little turn towards OT:
so what are using your opensolaris machines for?
The advantages of zfs?
 
Old 03-01-2010, 08:02 AM
Alan McKinnon
 
Default attach a perl script to daemon services

On Monday 01 March 2010 06:16:09 Harry Putnam wrote:
> Alan McKinnon <alan.mckinnon@gmail.com> writes:
> > FWIW, Solaris syslogd is like other basic tools on Solaris: standards
> > compliant in that it caters for the lowest common denominator that
> > comprises Unix. Which is to say, almost always useless for real work.
>
> A little turn towards OT:
> so what are using your opensolaris machines for?
> The advantages of zfs?


I had Solaris, not OpenSolaris. I've been steadily reducing my Solaris
machines (mostly because of the horrendous cost) and not many are left. Other
teams here still have quite a few, mostly for proprietary ISP monitoring
stuff, Oracle, VoIP billing systems - that kind of thing.



--
alan dot mckinnon at gmail dot com
 

Thread Tools




All times are GMT. The time now is 07:06 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org