root / plugins / oracle / oracle__connections @ 17f78427
Historique | Voir | Annoter | Télécharger (4,8 ko)
| 1 |
#!/usr/bin/perl -w |
|---|---|
| 2 |
# Plugin for monitor oracle connections. |
| 3 |
# |
| 4 |
# Licenced under GPL v2. |
| 5 |
# |
| 6 |
# Usage: |
| 7 |
# |
| 8 |
# Symlink into /etc/munin/plugins/ and add the monitored |
| 9 |
# database to the filename. e.g.: |
| 10 |
# |
| 11 |
# ln -s /usr/share/munin/plugins/oracle__connections \ |
| 12 |
# /etc/munin/plugins/oracle_<databasename>_connections |
| 13 |
# This should, however, be given through autoconf and suggest. |
| 14 |
# |
| 15 |
# If required, give username, password and/or oracle server |
| 16 |
# host through environment variables. |
| 17 |
# |
| 18 |
# |
| 19 |
# Parameters: |
| 20 |
# autoconf |
| 21 |
# config (required) |
| 22 |
# |
| 23 |
# Config variables: |
| 24 |
# |
| 25 |
# dbhost - Which database server to use. Defaults to |
| 26 |
# 'localhost'. |
| 27 |
# dbname - Which database to use. Defaults to orcl |
| 28 |
# dbuser - A oracle user account with read permission to |
| 29 |
# the v$session view. Defaults to |
| 30 |
# 'oracle'. Anyway, Munin must be told which user |
| 31 |
# this plugin should be run as. |
| 32 |
# dbpass - The corresponding password, if |
| 33 |
# applicable. Default to undef. |
| 34 |
# |
| 35 |
# showusers - If set to 1 show usernames and num. of connections. |
| 36 |
# Default is not show users (0). |
| 37 |
# Magic markers |
| 38 |
#%# family=auto |
| 39 |
#%# capabilities=autoconf |
| 40 |
|
| 41 |
use strict; |
| 42 |
use DBI; |
| 43 |
|
| 44 |
my $dbhost = $ENV{'dbhost'} || '127.0.0.1';
|
| 45 |
my $dbname = $ENV{'dbname'} || 'orcl';
|
| 46 |
my $dbuser = $ENV{'dbuser'} || 'oracle';
|
| 47 |
my $dbport = $ENV{'dbport'} || '1521';
|
| 48 |
my $dbpass = $ENV{'dbpass'} || '';
|
| 49 |
my $showusers = $ENV{'showusers'} || '0';
|
| 50 |
|
| 51 |
# Check for DBD::Oracle |
| 52 |
if (! eval "require DBD::Oracle;") {
|
| 53 |
exit 1; |
| 54 |
} |
| 55 |
|
| 56 |
my $dsn = "DBI:Oracle:dbname=$dbname;host=$dbhost;port=$dbport;sid=$dbname"; |
| 57 |
#print "$dsn\n"; |
| 58 |
my $dbh = DBI->connect ($dsn, $dbuser, |
| 59 |
$dbpass, |
| 60 |
{RaiseError =>1}) || die "";
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
if (exists $ARGV[0]) {
|
| 65 |
if ($ARGV[0] eq 'autoconf') {
|
| 66 |
# Check for DBD::Oracle |
| 67 |
if (! eval "require DBD::Oracle;") {
|
| 68 |
print "no (DBD::Oracle not found)"; |
| 69 |
exit 1; |
| 70 |
} |
| 71 |
if ($dbh) {
|
| 72 |
print "yes\n"; |
| 73 |
exit 0; |
| 74 |
} else {
|
| 75 |
print "no Unable to access Database $dbname on host $dbhost as user $dbuser.\nError returned was: ". $DBI::errstr; |
| 76 |
exit 1; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if ($ARGV[0] eq "config") {
|
| 81 |
my $sql_max = "select value from v\$parameter where name = 'sessions'"; |
| 82 |
my $sth_max = $dbh->prepare($sql_max); |
| 83 |
$sth_max->execute(); |
| 84 |
my ($max_connections) = $sth_max->fetchrow(); |
| 85 |
my $warning = int ($max_connections * 0.7); |
| 86 |
my $critical = int ($max_connections * 0.8); |
| 87 |
print "graph_title Oracle active connections from $dbname\n"; |
| 88 |
print "graph_args -l 0 --base 1000\n"; |
| 89 |
print "graph_vlabel Connections\n"; |
| 90 |
print "graph_category db\n"; |
| 91 |
print "graph_info Shows active oracle connections from $dbname\n"; |
| 92 |
print "graph_scale no\n"; |
| 93 |
if ( $showusers ) {
|
| 94 |
my $sql = "select username, count(username) from v\$session where username is not null group by username"; |
| 95 |
my $sth = $dbh->prepare($sql); |
| 96 |
$sth->execute(); |
| 97 |
my $setarea = "yes"; |
| 98 |
while ( my ($datname,$curr_conn) = $sth->fetchrow_array ) {
|
| 99 |
print "$datname.label $datname active connections\n"; |
| 100 |
print "$datname.info $datname active connections\n"; |
| 101 |
print "$datname.type GAUGE\n"; |
| 102 |
if ($setarea eq "yes") {
|
| 103 |
print "$datname.draw AREA\n"; |
| 104 |
$setarea=""; |
| 105 |
} else {
|
| 106 |
print "$datname.draw STACK\n"; |
| 107 |
} |
| 108 |
} |
| 109 |
} else {
|
| 110 |
print "connections.label active connections\n"; |
| 111 |
print "connections.info active connections\n"; |
| 112 |
print "connections.type GAUGE\n"; |
| 113 |
print "connections.warning $warning\n"; |
| 114 |
print "connections.critical $critical\n"; |
| 115 |
} |
| 116 |
print "total.label active connections\n"; |
| 117 |
print "total.info active connections\n"; |
| 118 |
print "total.type GAUGE\n"; |
| 119 |
print "total.warning $warning\n"; |
| 120 |
print "total.critical $critical\n"; |
| 121 |
print "max_connections.label Max. connections\n"; |
| 122 |
print "max_connections.info Max. connections\n"; |
| 123 |
print "max_connections.type GAUGE\n"; |
| 124 |
exit 0; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
my $sql_max = "select value from v\$parameter where name = 'sessions'"; |
| 130 |
my $sth_max = $dbh->prepare($sql_max); |
| 131 |
$sth_max->execute(); |
| 132 |
my ($max_connections) = $sth_max->fetchrow(); |
| 133 |
|
| 134 |
if ( $showusers ) {
|
| 135 |
my $sql = "select username, count(username) from v\$session where username is not null group by username"; |
| 136 |
my $sth = $dbh->prepare($sql); |
| 137 |
$sth->execute(); |
| 138 |
my $total = 0; |
| 139 |
while ( my ($datname,$curr_conn) = $sth->fetchrow_array ) {
|
| 140 |
print "$datname.value $curr_conn\n"; |
| 141 |
$total = $total+$curr_conn; |
| 142 |
} |
| 143 |
print "total.value $total\n"; |
| 144 |
} else {
|
| 145 |
my $sql = "select count(username) from v\$session where username is not null"; |
| 146 |
my $sth = $dbh->prepare($sql); |
| 147 |
$sth->execute(); |
| 148 |
my ($curr_conn) = $sth->fetchrow_array; |
| 149 |
print "connections.value $curr_conn\n"; |
| 150 |
} |
| 151 |
|
| 152 |
print "max_connections.value $max_connections\n"; |
| 153 |
|
