~ubuntu-branches/ubuntu/gutsy/munin/gutsy

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
#!@@PERL@@ -w
#
# Plugin to monitor sybase database space usage
#
# Parameters:
#
# 	config
#
# You need to add the user to all the databases you want monitored.
#
# Configuration variables:
#
# 	SYBASE       - Sybase home
# 	SYBASE_USER  - Username
# 	SYBASE_PASS  - Password
# 	SYBASE_HOST  - Host
# 	
# 	 $Log: sybase_space.in,v $
# 	 Revision 1.1  2004/01/02 18:50:00  jimmyo
# 	 Renamed occurrances of lrrd -> munin
# 	
# 	 Revision 1.1.1.1  2004/01/02 15:18:07  jimmyo
# 	 Import of LRRD CVS tree after renaming to Munin
# 	
# 	 Revision 1.3  2003/11/10 18:41:33  jimmyo
# 	 Removed Data::Dumper dependency.
# 	
# 	 Revision 1.2  2003/11/07 17:43:16  jimmyo
# 	 Cleanups and log entries
# 	
#
#
#%# family=manual
#%# capabilities=

use strict;
use DBD::Sybase;
use DBI;

my $user = $ENV{SYBASE_USER} || "monitor";
my $pass = $ENV{SYBASE_PASS} || "monitor";
my $host = $ENV{SYBASE_HOST} || "localhost";
my $db   = $ENV{SYBASE_DB}   || "master";

$ENV{SYBASE} = $ENV{SYBASE}  || "/usr/local/sybase";

my $dbh = DBI->connect ("dbi:Sybase:$db;host=$host", $user, $pass);

if (!$dbh) 
{
	die "Could not run DBI::connect\n";
}

my $databases = &list_dbs ($dbh);

if (exists $ARGV[0] and $ARGV[0] =~ /^config$/)
{
	print "host_name sybase-i.fileflow.com\n";
	print "graph_title Sybase database space usage\n";
	print "graph_args -u 100 -l 0\n";
	print "graph_vlabel %\n";

	foreach my $db (keys %{$databases})
	{
		print "$db.label $db\n";
		print "$db.type GAUGE\n";
		print "$db.warning 85\n";
		print "$db.critical 95\n";
	}
	exit 0;
}

my $db_info;

foreach my $db (keys %{$databases})
{
	$db_info = &space_db ($dbh, $db, $db_info);
}

foreach my $db (keys %{$db_info})
{
	#print "$db $db_info->{$db}->{used} / $db_info->{$db}->{total} = ", $db_info->{$db}->{used}*100/$db_info->{$db}->{total}, ".\n";
	print "$db.value ", $db_info->{$db}->{used}*100/$db_info->{$db}->{total}, "\n";
}

1;

sub list_dbs
{
	my $h   = shift;
	my $dbs = undef;

	if (! $h->do ("use master"))
	{
		die "Error: could not \"use master\"...\n";
	}
	my $sth = $dbh->prepare ("select name from sysdatabases");
	my $rv  = $sth->execute;
	if (! $rv)
	{
		die "Error: could not run \"select name from sysdatabases\"...\n";
	}

	$dbs = $sth->fetchall_hashref ("name");

	return $dbs;
}

sub space_db
{
	my $h   = shift;
	my $db  = shift;
	my $dbs = shift;

	if (! $h->do ("use $db"))
	{
		die "Error: could not \"use $db\"...\n";
	}
	my $sth = $dbh->prepare ("sp_spaceused");
	my $rv  = $sth->execute;

	if (! $rv)
	{
		die "Error: could not use \"sp_spaceused\"...\n";
	}

	do {
		while (my $d = $sth->fetchrow_arrayref)
		{
			#print join ('|',@{$d}), "...\n";
			if ($d->[0] =~ /^$db$/)
			{
				$dbs->{$db}->{total} = &bytes ($d->[1]);
			}
			elsif (!$d->[0])
			{
				next;
			}
			else
			{
				$dbs->{$db}->{used} = &bytes ($d->[0]);
			}
		}
	} while ($sth->{syb_more_results});

	return $dbs;
}

sub bytes
{
	my $val = shift;

	if ($val =~ /^\s*([\d\.]+)\s*kb\s*$/i)
	{
		$val = $1*1024;
	}
	elsif ($val =~ /^\s*([\d\.]+)\s*mb\s*$/i)
	{
		$val = $1*1024*1024;
	}
	elsif ($val =~ /^\s*([\d\.]+)\s*gb\s*$/i)
	{
		$val = $1*1024*1024*1024;
	}
	return $val;
}


# vim:syntax=perl