File: Syslog.pm

package info (click to toggle)
razor 1%3A2.85-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 848 kB
  • sloc: perl: 4,694; ansic: 178; makefile: 19; sh: 3
file content (179 lines) | stat: -rw-r--r-- 3,793 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package Razor2::Syslog;

use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
use IO::Socket;
use IO::File;
use Data::Dumper;

require Exporter;

@ISA = qw(Exporter AutoLoader);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
@EXPORT = qw(
	
);
$VERSION = '0.03';


# Preloaded methods go here.

my %syslog_priorities=(
	emerg   => 0,
	alert   => 1,
	crit    => 2,
	err     => 3,
	warning => 4,
    notice  => 5,
    info    => 6,
	debug   => 7
);

my %syslog_facilities=(
	kern	=> 0,
	user	=> 1,
	mail	=> 2,
	daemon	=> 3,	
	auth	=> 4,
	syslog	=> 5,
	lpr	=> 6,
	news 	=> 7,
	uucp 	=> 8,
	cron	=> 9,
	authpriv=> 10,
	ftp	=> 11,
	local0	=> 16,
	local1	=> 17,
	local2	=> 18,
	local3	=> 19,
	local4	=> 20,
	local5	=> 21,
	local6	=> 22,
);


sub new {

  my $class = shift;
  my $name = $0;
  if($name =~ /.+\/(.+)/){
     $name = $1;
  }
  my $self = { Name     => $name,
               Facility => 'local5',
               Priority => 'err',
               SyslogPort    => 514,
               SyslogHost    => '127.0.0.1'};
  bless $self,$class;
  my %par = @_;

  foreach (keys %par){
    $self->{$_}=$par{$_};
  }

  my $sock=new IO::Socket::INET(PeerAddr => $$self{SyslogHost},
                        PeerPort => $$self{SyslogPort},
                        Proto    => 'udp');
  die "Socket could not be created : $!\n" unless $sock;

  $self->{sock} = $sock;

  return $self;

}


sub send {

  my $self = shift;
  my $msg=shift;
  my %par = @_;
  my %local=%$self;

  foreach (keys %par){
    $local{$_}=$par{$_};
  }

  my $pid=$$;
  my $facility_i=$syslog_facilities{$local{Facility}};
  my $priority_i=$syslog_priorities{$local{Priority}};  

  if(!defined $facility_i){
    $facility_i=21;
  }

  if(!defined $priority_i){
    $priority_i=4;
  }

  my $d=(($facility_i<<3)|($priority_i));
  my $message = "<$d>$local{Name}\[$pid\]: $msg";

  my $sock = $self->{sock};

  # Send the message to the socket directly.
  $sock->send($message);
  # Flush the socket, to ensure that messages don't arrive combined into one packet.
  $sock->flush;

}


# Autoload methods go after =cut, and are processed by the autosplit program.

1;
__END__
# Below is the stub of documentation for your module. You better edit it!

=head1 NAME

Razor2::Syslog -- Syslog support for Razor2

=head1 SYNOPSIS

  use Razor2::Syslog;
  my $s=new Razor2::Syslog(Facility=>'local4',Priority=>'debug');
  $s->send('see this in syslog',Priority=>'info');

=head1 DESCRIPTION

This module has been derived from Net::Syslog. Some optimizations were
made to Net::Syslog, in particular support for keeping a socket open. What
follows is the documentation for Net::Syslog, which completely applies to
this module.

Net::Syslog implements the intra-host syslog forwarding protocol. It is
not intended to replace the Sys::Syslog or Unix::Syslog modules, but
instead to provide a method of using syslog when a local syslogd is
unavailable or when you don't want to write syslog messages to the
local syslog.

The new call sets up default values, any of which can be overridden in the
send call.  Keys (listed with default values) are:

	Name		<calling script name>
	Facility 	local5
	Priority 	err
	SyslogPort    	514
	SyslogHost    	127.0.0.1

Valid Facilities are:
	kern, user, mail, daemon, auth, syslog, lpr, news, uucp, cron, 
	authpriv, ftp, local0, local1, local2, local3, local4, local5, local6

Valid Priorities are:
	emerg, alert, crit, err, warning, notice, info, debug



=head1 AUTHOR

 Les Howard, les@lesandchris.com
 Vipul Ved Prakash, mail@vipul.net

=head1 SEE ALSO

syslog(3), Sys::Syslog(3), syslogd(8), Unix::Syslog(3), IO::Socket, perl(1)

=cut