/usr/share/perl5/Mon/Protocol.pm is in mon-client 1.2.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | =head1 NAME
Mon::Protocol - Methods for parsing / dumping a protocol block
=head1 SYNOPSIS
use Mon::Protocol;
=head1 DESCRIPTION
=head1 METHODS
=over 4
=item new
Creates a new object. A hash can be supplied which sets the
default values. An example which contains all of the variables
that you can initialize:
$c = new Mon::Protocol;
=item dump_data
Returns the current internal structure as a string dump suitable for passing
to C<parse_data>.
=item C<parse_data>
Parses a command block (from begin_block to end_block), as generated by
dump_data.
=item C<type>(I<new_type>)
Sets or returns the type of the current command block. See @TYPES for valid
type codes.
In the future, it is possible that this module will perform additional
checking based on the type, for now it is left to the application to interpret
this.
=item C<get_section_list>
Returns an array containing all section names within the block.
=item C<get_section>(I<section_name>)
Returns a hash containing the key/value pairs of the specific section.
=item C<delete_section>(I<section_name>)
Completely removes the specified section from the block.
=item C<add_to_section>(I<section_name>,I<$hash_ref>)
Adds the key/value pairs in the hash to the specified section.
$foo->add_to_section("_hostgroup", { "ns1.baz.com" -> "ok" });
=item C<delete_from_section>(I<section_name>,I<$key>)
Deletes the key/value pair from the section.
$foo->delete_from_section("_hostgroup", "ns1.baz.com");
=item C<error>
Should any of the functions return an error (-1), this function can be used to
retrieve a more elaborate error message and to reset the internal error state.
=back
=cut
#
# Perl module for parsing / dumping a mon protocol block
#
# $Id: Protocol.pm,v 1.1.1.1 2004/06/18 14:25:16 trockij Exp $
#
# Copyright (C) 1999 Lars Marowsky-Brée <lmb@teuto.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
package Mon::Protocol;
require Exporter;
require 5.004;
use IO::File;
use Socket;
sub new;
sub dump_data;
sub parse_data;
sub type;
sub get_section;
sub get_section_list;
sub add_to_section;
sub delete_section;
sub delete_from_section;
sub error;
sub DESTROY;
sub _esc_str;
sub _un_esc_str;
@ISA = qw(Exporter);
@EXPORT_OK = qw($VERSION @);
$VERSION = "1.0000";
@TYPES = qw(cmd_monitor cmd_alert cmd_logger res_monitor res_alert res_logger);
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = {};
my %vars = @_;
$self->{'type'} = $vars{'type'} || "UNKNOWN";
bless ($self, $class);
return $self;
}
sub dump_data {
my ($self) = shift;
my ($tmp);
$tmp.="begin_block=".$self->{'type'}."\n";
my ($section);
foreach $section (sort keys %{$self->{'data'}}) {
$tmp.="begin=".$section."\n";
my ($key,$data);
while ( ($key,$data) = each %{$self->{'data'}->{$section}} ) {
$tmp .= "$key=". _esc_str ($data) . "\n";
}
$tmp.="end=".$section."\n";
}
$tmp.="end_block=".$self->{'type'}."\n";
return $tmp;
}
sub parse_data {
my ($self) = shift;
my ($raw) = @_;
my (@l) = split(/\n/o,$raw);
my ($l,%tmp,$type);
$l = shift @l;
if ($l =~ /^begin_block=(\S+)$/oi) {
$type = lc($1);
} else {
$self->{'error'} = "No begin_block found";
return -1;
}
my ($section,$in_section) = ("",0);
LINE: while ($l = shift @l) {
next if ($l =~ /^\s*$/o);
if ($in_section == 0) {
if ($l =~ /^begin=(\S+)$/oi) {
$section = lc($1);
$in_section = 1;
} elsif ($l =~ /^end_block=(\S+)$/oi) {
if (lc($1) eq $type) {
$in_section = -1;
last LINE;
} else {
$self->{'error'} = "end_block does not match begin.";
return -1;
}
} else {
$self->{'error'} = "Garbled input at global level.";
return -1;
}
} else {
if ($l =~ /^end=(\S+)$/oi) {
if (lc($1) eq $section) {
$in_section = 0;
$section = "";
next LINE;
} else {
$self->{'error'} = "end section does not match begin.";
return -1;
}
}
my ($key,$value);
if (($key,$value) = $l =~ /^([^=]+)=(.*)/o) {
$key = lc($key);
$tmp{$section}{$key} = _un_esc_str ($value);
} else {
$self->{'error'} = "Garbled input at section level: $l";
return -1;
}
}
}
$self->{'type'} = $type;
%{$self->{'data'}} = %tmp;
return 0;
}
sub type {
my ($self) = shift;
if (@_) {
my ($type) = lc(shift);
if (grep($_ eq $type,@TYPES)) {
$self->{'type'} = $type;
} else {
$self->{'type'} = "UNKNOWN";
$self->{'error'} = "Unknown type supplied.";
return -1;
}
}
return $self->{'type'};
}
sub get_section {
my ($self) = shift;
my ($section) = lc(shift);
if (defined($self->{'data'}->{$section})) {
return %{$self->{'data'}->{$section}};
} else {
$self->{'error'} = "$section: No such section.";
return -1;
}
}
sub get_section_list {
my ($self) = shift;
return sort keys %{$self->{'data'}};
}
sub add_to_section {
my ($self) = shift;
my $section = lc(shift);
my ($rdata) = @_;
my ($key,$value);
while ( ($key,$value) = each %$rdata) {
$key = lc($key);
$self->{'data'}->{$section}->{$key} = $value;
}
return 1;
}
sub delete_section {
my ($self) = shift;
my ($section) = lc(shift);
if (defined($self->{'data'}->{$section})) {
delete $self->{'data'}->{$section};
return 0;
} else {
$self->{'error'} = "$section: No such section.";
return -1;
}
}
sub delete_from_section {
my ($self) = shift;
my ($section) = lc(shift);
my ($key) = lc(shift);
if (defined($self->{'data'}->{$section}->{$key})) {
delete $self->{'data'}->{$section}->{$key};
return 0;
} else {
$self->{'error'} = "$section/$key: No such key in section.";
return -1;
}
}
sub error {
my ($self) = shift;
my $err = $self->{'error'};
$self->{'error'}= "";
return $err;
}
sub DESTROY {
my $self = shift;
}
#
# convert a string to a hex-escaped string, returning
# the escaped string.
#
# $str is the string to be escaped
# if $inquotes is true, backslashes are doubled, making
# the escaped string suitable to be enclosed in
# single quotes and later passed to split
# For example, var='quoted value'
#
sub _esc_str {
my $str = shift;
my $inquotes = shift;
my $escstr = "";
for (my $i = 0; $i < length ($str); $i++)
{
my $c = substr ($str, $i, 1);
if (ord ($c) <= 32 ||
ord ($c) > 126 ||
$c eq "\"" ||
$c eq "\'")
{
$c = sprintf ("\\%02x", ord($c));
}
elsif ($inquotes && $c eq "\\")
{
$c = "\\\\";
}
$escstr .= $c;
}
$escstr;
}
#
# convert a hex-escaped string into an unescaped string,
# returning the unescaped string
#
sub _un_esc_str {
my $str = shift;
$str =~ s{\\([0-9a-f]{2})}{chr(hex($1))}eg;
$str;
}
1;
|