~ubuntu-branches/ubuntu/trusty/net-snmp/trusty

« back to all changes in this revision

Viewing changes to perl/agent/agent.pm

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2004-09-13 12:06:21 UTC
  • Revision ID: james.westby@ubuntu.com-20040913120621-g952ntonlleihcvm
Tags: upstream-5.1.1
ImportĀ upstreamĀ versionĀ 5.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package NetSNMP::agent;
 
2
 
 
3
use strict;
 
4
use Carp;
 
5
 
 
6
require Exporter;
 
7
require DynaLoader;
 
8
use AutoLoader;
 
9
 
 
10
use NetSNMP::default_store (':all');
 
11
use NetSNMP::agent::default_store (':all');
 
12
use NetSNMP::OID (':all');
 
13
use NetSNMP::agent::netsnmp_request_infoPtr;
 
14
 
 
15
use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK @EXPORT $VERSION $AUTOLOAD);
 
16
 
 
17
@ISA = qw(Exporter AutoLoader DynaLoader);
 
18
 
 
19
# Items to export into callers namespace by default. Note: do not export
 
20
# names by default without a very good reason. Use EXPORT_OK instead.
 
21
# Do not simply export all your public functions/methods/constants.
 
22
 
 
23
# This allows declaration       use NetSNMP::agent ':all';
 
24
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
 
25
# will save memory.
 
26
%EXPORT_TAGS = ( 'all' => [ qw(
 
27
        MODE_GET
 
28
        MODE_GETBULK
 
29
        MODE_GETNEXT
 
30
        MODE_SET_ACTION
 
31
        MODE_SET_BEGIN
 
32
        MODE_SET_COMMIT
 
33
        MODE_SET_FREE
 
34
        MODE_SET_RESERVE1
 
35
        MODE_SET_RESERVE2
 
36
        MODE_SET_UNDO
 
37
) ] );
 
38
 
 
39
@EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
 
40
 
 
41
@EXPORT = qw(
 
42
        MODE_GET
 
43
        MODE_GETBULK
 
44
        MODE_GETNEXT
 
45
        MODE_SET_ACTION
 
46
        MODE_SET_BEGIN
 
47
        MODE_SET_COMMIT
 
48
        MODE_SET_FREE
 
49
        MODE_SET_RESERVE1
 
50
        MODE_SET_RESERVE2
 
51
        MODE_SET_UNDO
 
52
);
 
53
$VERSION = '5.1.1';
 
54
 
 
55
sub AUTOLOAD {
 
56
    # This AUTOLOAD is used to 'autoload' constants from the constant()
 
57
    # XS function.  If a constant is not found then control is passed
 
58
    # to the AUTOLOAD in AutoLoader.
 
59
 
 
60
    my $constname;
 
61
    ($constname = $AUTOLOAD) =~ s/.*:://;
 
62
    croak "& not defined" if $constname eq 'constant';
 
63
    my $val = constant($constname, @_ ? $_[0] : 0);
 
64
    if ($! != 0) {
 
65
        if ($! =~ /Invalid/ || $!{EINVAL}) {
 
66
            $AutoLoader::AUTOLOAD = $AUTOLOAD;
 
67
            goto &AutoLoader::AUTOLOAD;
 
68
        }
 
69
        else {
 
70
            croak "Your vendor has not defined NetSNMP::agent macro $constname";
 
71
        }
 
72
    }
 
73
    {
 
74
        no strict 'refs';
 
75
        # Fixed between 5.005_53 and 5.005_61
 
76
#       if ($] >= 5.00561) {
 
77
#           *$AUTOLOAD = sub () { $val };
 
78
#       }
 
79
#       else {
 
80
            *$AUTOLOAD = sub { $val };
 
81
#       }
 
82
    }
 
83
    goto &$AUTOLOAD;
 
84
}
 
85
 
 
86
{
 
87
    my $haveinit = 0;
 
88
 
 
89
    sub mark_init_agent_done {
 
90
        $haveinit = 1;
 
91
    }
 
92
 
 
93
    sub maybe_init_agent {
 
94
        return if ($haveinit);
 
95
        $haveinit = 1;
 
96
 
 
97
        snmp_enable_stderrlog();
 
98
        my $flags = $_[0];
 
99
        if ($flags->{'AgentX'}) {
 
100
            netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
 
101
        }
 
102
        init_agent($flags->{'Name'} || "perl");
 
103
        if ($flags->{'Ports'}) {
 
104
            netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_PORTS, $flags->{'Ports'});
 
105
        }
 
106
        init_mib();
 
107
    }
 
108
}
 
109
 
 
110
{
 
111
    my $haveinit = 0;
 
112
 
 
113
    sub mark_init_lib_done {
 
114
        $haveinit = 1;
 
115
    }
 
116
 
 
117
    sub maybe_init_lib {
 
118
        return if ($haveinit);
 
119
        $haveinit = 1;
 
120
 
 
121
        my $flags = $_[0];
 
122
        init_snmp($flags->{'Name'} || "perl");
 
123
        if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE) != 1) {
 
124
            init_master_agent();
 
125
        }
 
126
    }
 
127
}
 
128
 
 
129
sub new {
 
130
    my $type = shift;
 
131
    my ($self);
 
132
    %$self = @_;
 
133
    bless($self, $type);
 
134
    if ($self->{'dont_init_agent'}) {
 
135
        $self->mark_init_agent_done();
 
136
    } else {
 
137
        $self->maybe_init_agent();
 
138
    }
 
139
    if ($self->{'dont_init_lib'}) {
 
140
        $self->mark_init_lib_done();
 
141
    }
 
142
    return $self;
 
143
}
 
144
 
 
145
sub register($$$$) {
 
146
    my ($self, $name, $oid, $sub) = @_;
 
147
    my $reg = NetSNMP::agent::netsnmp_handler_registration::new($name, $oid, $sub);
 
148
    $reg->register() if ($reg);
 
149
    return $reg;
 
150
}
 
151
 
 
152
sub main_loop {
 
153
    my $self = shift;
 
154
    while(1) {
 
155
        $self->agent_check_and_process(1);
 
156
    }
 
157
}
 
158
 
 
159
sub agent_check_and_process {
 
160
    my ($self, $blocking) = @_;
 
161
    $self->maybe_init_lib();
 
162
    __agent_check_and_process($blocking || 0);
 
163
}
 
164
 
 
165
bootstrap NetSNMP::agent $VERSION;
 
166
 
 
167
# Preloaded methods go here.
 
168
 
 
169
# Autoload methods go after =cut, and are processed by the autosplit program.
 
170
 
 
171
1;
 
172
__END__
 
173
# Below is stub documentation for your module. You better edit it!
 
174
 
 
175
=head1 NAME
 
176
 
 
177
NetSNMP::agent - Perl extension for the net-snmp agent.
 
178
 
 
179
=head1 SYNOPSIS
 
180
 
 
181
  use NetSNMP::agent;
 
182
  my $agent = new NetSNMP::agent('Name' -> 'my_agent_name');
 
183
  $agent->register("a_name", ".1.3.6.1.2.1", \&myhandler);
 
184
  $agent->main_loop();
 
185
 
 
186
    --- or, within the net-snmp snmpd.conf file: ---
 
187
 
 
188
  perl $agent->register("a_name", ".1.3.6.1.2.1", \&myhandler);
 
189
 
 
190
=head1 DESCRIPTION
 
191
 
 
192
This module implements a snmp agent and/or can be embedded within the
 
193
net-snmp agent.
 
194
 
 
195
=head1 AUTHOR
 
196
 
 
197
Please mail the net-snmp-users@lists.sourceforge.net mailing list for
 
198
help, questions or comments about this module.
 
199
 
 
200
Wes Hardaker, hardaker@users.sourceforge.net
 
201
 
 
202
=head1 SEE ALSO
 
203
 
 
204
perl(1).
 
205
 
 
206
=cut