File: munin-async

package info (click to toggle)
munin 2.999.14-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 10,036 kB
  • sloc: perl: 31,052; sh: 12,473; java: 1,924; python: 852; makefile: 336; ruby: 232; awk: 169; xml: 45; sql: 11
file content (167 lines) | stat: -rwxr-xr-x 4,224 bytes parent folder | download
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
#!/usr/bin/perl
#
# Copyright (C) 2010 Steve Schnepp
#
# 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; version 2 dated June,
# 1991.
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

use strict;
use warnings;

use English qw(-no_match_vars);
use Sys::Hostname;
use Data::Dumper;
use Getopt::Long;
use Pod::Usage;

use Munin::Node::SpoolReader;
use Munin::Node::SpoolWriter;
use Munin::Common::Defaults;
use Munin::Common::Logger;
use Munin::Common::Utils qw( is_valid_hostname );

# Disable buffering
$OUTPUT_AUTOFLUSH = 1;

my $SPOOLDIR = $Munin::Common::Defaults::MUNIN_SPOOLDIR . "/async";
my $hostname;
my $overridehost;

my $spoolfetch;
my $vectorfetch;
my $cleanup;
my $cleanupandexit;

my $verbose;
my $debug;
my $screen;
my $help;

GetOptions(
        "spooldir|s=s" => \$SPOOLDIR,
        "hostname=s" => \$overridehost,
        "cleanup" => \$cleanup,
        "cleanupandexit" => \$cleanupandexit,

        "spoolfetch" => \$spoolfetch,
        "vectorfetch" => \$vectorfetch,

        "help|h" => \$help,
        "verbose|v" => \$verbose,
        "debug" => \$debug,
        "screen" => \$screen,
) or pod2usage(1);
if ($help) {
        pod2usage(1);
}


if ($cleanupandexit) {
	cleanup();
	exit;
}

if ($overridehost) {
    if (! is_valid_hostname($overridehost)) {
        CRITICAL(sprintf("invalid hostname: %s\n", $overridehost));
        exit(65);
    }
}

if ( $verbose || $debug || $screen ) {
    my %log;
    $log{output} = 'screen' if $screen;
    $log{level}  = 'info'   if $verbose;
    $log{level}  = 'debug'  if $debug;
    Munin::Common::Logger::configure(%log);
}

# Use STDIN/STDOUT, in order to be:
# 1. secure over internet (SSH), munin-node needs only
#    to listen on localhost:4949
# 2. very simple to launch

my $spoolreader = Munin::Node::SpoolReader->new(
	spooldir => $SPOOLDIR,
);

$hostname = $spoolreader->get_metadata("hostname") || hostname();
$hostname = $overridehost if $overridehost;
chomp($hostname);

INFO("Starting");

print "# munin node at $hostname\n";

while (my $line = <>) {
	if ($line =~ m/^list/) {
		print $spoolreader->list();
	} elsif ($line =~ m/^config (\w+)/) {
		# XXX - Vector-fetching is disabled for now
		print ".\n";
	} elsif ($vectorfetch && $line =~ m/^fetch (\w+)/) {
		# Fetching all values since last time

		# XXX - Vector-fetching is disabled for now
		print ".\n";
	} elsif ($line =~ m/^spoolfetch (-?\d+)/) {
		my $last_epoch = $1;
		print $spoolreader->fetch($last_epoch);
		print ".\n";
	} elsif ($spoolfetch && $line =~ m/^cap/) {
		print "cap spool\n";
	} elsif ($line =~ m/^quit/) {
		last;
	} else {
		print "# Unknown command.\n";
	}
}

cleanup() if $cleanup;
exit;

sub cleanup {
	my $spoolwriter = Munin::Node::SpoolWriter->new(
		spooldir => $SPOOLDIR,
	);
	$spoolwriter->cleanup();
}

__END__

=head1 NAME

munin-async - A program to replay spooled munin-node calls

=head1 SYNOPSIS

munin-async [options]

 Options:
     -s --spooldir <spooldir>   Directory for spooled data [@@SPOOLDIR@@/async]
     --hostname <hostname>      Overrides the hostname [`hostname`]
     --cleanup                  Clean up the spooldir after interactive session completes
     --cleanupandexit           Clean up the spooldir and exit (non-interactive)

     --spoolfetch               Enables the "spool" capability [no]
     --vectorfetch              Enables the "vectorized" fetching capability [no]
                                  Note that without this flag, the "fetch"
				  command is disabled.

     --screen                   Log to screen instead of syslog
     --debug                    Log debug messages
     -v --verbose               Be verbose
     -h --help                  View this message