root / plugins / kamailio / kamailio_mysql_shared_memory @ c5ab6538
Historique | Voir | Annoter | Télécharger (5,36 ko)
| 1 | 72e4561a | Oussama Hammami | #!/usr/bin/perl |
|---|---|---|---|
| 2 | # -*- perl -*- |
||
| 3 | |||
| 4 | =head1 NAME |
||
| 5 | |||
| 6 | Munin plugin to monitor the usage of shared memory in Kamailio using 'statistics' table. |
||
| 7 | |||
| 8 | =head1 APPLICABLE SYSTEMS |
||
| 9 | |||
| 10 | It requires MySQL 'statistics' table created in Kamailio database. |
||
| 11 | http://siremis.asipto.com/install-charts-panel/ |
||
| 12 | |||
| 13 | =head1 CONFIGURATION |
||
| 14 | |||
| 15 | [kamailio*] |
||
| 16 | user root |
||
| 17 | group root |
||
| 18 | env.mysql <optional-override-of-mysqladmin-path> |
||
| 19 | env.mysqlauth -u<User> -p<Password> |
||
| 20 | env.kamailiodb <kamailio data base> |
||
| 21 | |||
| 22 | It is most usual that root must run the mysql command. |
||
| 23 | |||
| 24 | =head2 Proxy config |
||
| 25 | |||
| 26 | use rtimer module to run periodically a route. In that route you insert the values in database. |
||
| 27 | |||
| 28 | SIP Proxy configuration file: |
||
| 29 | |||
| 30 | loadmodule "rtimer.so" |
||
| 31 | loadmodule "sqlops.so" |
||
| 32 | loadmodule "cfgutils.so" |
||
| 33 | ... |
||
| 34 | modparam("rtimer", "timer", "name=tst;interval=300;mode=1;")
|
||
| 35 | modparam("rtimer", "exec", "timer=tst;route=8")
|
||
| 36 | modparam("sqlops","sqlcon","ca=>mysql://openser:openserrw@localhost/openser")
|
||
| 37 | ... |
||
| 38 | route[8] {
|
||
| 39 | sql_query("ca",
|
||
| 40 | "insert into statistics (time_stamp,random,shm_used_size,shm_real_used_size, |
||
| 41 | shm_max_used_size,shm_free_used_size,ul_users,ul_contacts) values ($Ts, |
||
| 42 | $RANDOM,$stat(used_size),$stat(real_used_size),$stat(max_used_size), |
||
| 43 | $stat(free_size),$stat(location-users),$stat(location-contacts))","ra"); |
||
| 44 | } |
||
| 45 | |||
| 46 | Note: second parameter of sql_query(...) is a single line. Next version, based on SIP-Router.org project will support string parameters broken in multiple lines. |
||
| 47 | |||
| 48 | =head2 Database |
||
| 49 | |||
| 50 | You have to create a new table in Kamailio (OpenSER) database: |
||
| 51 | |||
| 52 | CREATE TABLE `statistics` ( |
||
| 53 | `id` int(10) unsigned NOT NULL auto_increment, |
||
| 54 | `time_stamp` int(10) unsigned NOT NULL default '0', |
||
| 55 | `random` int(10) unsigned NOT NULL default '0', |
||
| 56 | `shm_used_size` int(10) unsigned NOT NULL default '0', |
||
| 57 | `shm_real_used_size` int(10) unsigned NOT NULL default '0', |
||
| 58 | `shm_max_used_size` int(10) unsigned NOT NULL default '0', |
||
| 59 | `shm_free_used_size` int(10) unsigned NOT NULL default '0', |
||
| 60 | `ul_users` int(10) unsigned NOT NULL default '0', |
||
| 61 | `ul_contacts` int(10) unsigned NOT NULL default '0', |
||
| 62 | PRIMARY KEY (`id`) |
||
| 63 | ) ENGINE=MyISAM; |
||
| 64 | |||
| 65 | Now all is ready for Kamailio (OpenSER), you can restart it. |
||
| 66 | |||
| 67 | =head1 BUGS |
||
| 68 | |||
| 69 | None known |
||
| 70 | |||
| 71 | =head1 AUTHOR |
||
| 72 | |||
| 73 | Copyright 2012 - Voxtrot <www.voxtrot.com> |
||
| 74 | Oussama Hammami <oussamacvoxtrot.com> |
||
| 75 | |||
| 76 | =head1 LICENSE |
||
| 77 | |||
| 78 | GPLv2 |
||
| 79 | |||
| 80 | =head1 VERSION |
||
| 81 | |||
| 82 | $Id: kamailio_mysql_shared_memory 2012-04-19 11:24 $ |
||
| 83 | |||
| 84 | =head1 MAGIC MARKERS |
||
| 85 | |||
| 86 | #%# family=manual |
||
| 87 | #%# capabilities=autoconf |
||
| 88 | |||
| 89 | =cut |
||
| 90 | |||
| 91 | use strict; |
||
| 92 | |||
| 93 | my $MYSQLADMIN = $ENV{mysql} || "mysql";
|
||
| 94 | my $COMMAND = "$MYSQLADMIN $ENV{mysqlauth} $ENV{kamailiodb} -e 'select * from statistics order by id desc limit 1\\G'";
|
||
| 95 | |||
| 96 | my %WANTED = ( "shm_free_used_size" => "shmem_total", |
||
| 97 | "shm_real_used_size" => "shmem_real_used", |
||
| 98 | "shm_used_size" => "shmem_used", |
||
| 99 | ); |
||
| 100 | |||
| 101 | my %VALUE = ( "shmem_total" => 0, |
||
| 102 | "shmem_real_used" => 0, |
||
| 103 | "shmem_used" => 0, |
||
| 104 | ); |
||
| 105 | |||
| 106 | my $arg = shift(); |
||
| 107 | |||
| 108 | if ($arg eq 'config') {
|
||
| 109 | print_config(); |
||
| 110 | exit(); |
||
| 111 | } elsif ($arg eq 'autoconf') {
|
||
| 112 | unless (test_service() ) {
|
||
| 113 | print "yes\n"; |
||
| 114 | } else {
|
||
| 115 | print "no\n"; |
||
| 116 | } |
||
| 117 | exit 0; |
||
| 118 | } |
||
| 119 | |||
| 120 | open(SERVICE, "$COMMAND |") |
||
| 121 | or die("Could not execute '$COMMAND': $!");
|
||
| 122 | |||
| 123 | while (<SERVICE>) {
|
||
| 124 | my ($k, $v) = (m/(\w+).*?(\d+(?:\.\d+)?)/); |
||
| 125 | next unless ($k); |
||
| 126 | if (exists $WANTED{$k} ) {
|
||
| 127 | $VALUE{$WANTED{$k}}=$v;
|
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | close(SERVICE); |
||
| 132 | |||
| 133 | $VALUE{"shmem_total"}+=$VALUE{"shmem_real_used"};
|
||
| 134 | |||
| 135 | for my $key (keys %VALUE) {
|
||
| 136 | print ("$key.value $VALUE{$key}\n");
|
||
| 137 | } |
||
| 138 | |||
| 139 | sub print_config {
|
||
| 140 | print ("graph_title Kamailio Shared Memory\n");
|
||
| 141 | # Arguments to "rrdtool graph". In this case, tell it that the |
||
| 142 | # lower limit of the graph is '0', and that 1k=1000 (not 1024). |
||
| 143 | print("graph_args --base 1024 --lower-limit 0\n");
|
||
| 144 | print("graph_vlabel MB\n");
|
||
| 145 | print("graph_scale no\n");
|
||
| 146 | print("graph_category kamailio\n");
|
||
| 147 | print("graph_info The graph describes the usage of shared memory.\n");
|
||
| 148 | print("shmem_total.label total\n");
|
||
| 149 | print("shmem_used.label used\n");
|
||
| 150 | print("shmem_real_used.label real used\n");
|
||
| 151 | print("shmem_total.info Average total shared memory used for the five minutes.\n");
|
||
| 152 | print("shmem_used.info Average used shared memory for the five minutes.\n");
|
||
| 153 | print("shmem_real_used.info Average real used shared memory for the five minutes.\n");
|
||
| 154 | print("graph_order shmem_total shmem_used shmem_real_used\n");
|
||
| 155 | print("shmem_total.type GAUGE\n");
|
||
| 156 | print("shmem_used.type GAUGE\n");
|
||
| 157 | print("shmem_real_used.type GAUGE\n");
|
||
| 158 | print("shmem_total.draw AREA\n");
|
||
| 159 | print("shmem_used.draw AREA\n");
|
||
| 160 | print("shmem_real_used.draw LINE1\n");
|
||
| 161 | print("shmem_total.colour 11DB58\n");
|
||
| 162 | print("shmem_used.colour F7CB03\n");
|
||
| 163 | print("shmem_real_used.colour 990000\n");
|
||
| 164 | # Ensure min values (useful when using 'DERIVE' as 'type'). |
||
| 165 | print("shmem_total.min 0\n");
|
||
| 166 | print("shmem_used.min 0\n");
|
||
| 167 | print("shmem_real_used.min 0\n");
|
||
| 168 | # Divide the got value by 1048576 to get MB. |
||
| 169 | print("shmem_total.cdef shmem_total,1048576,/\n");
|
||
| 170 | print("shmem_used.cdef shmem_used,1048576,/\n");
|
||
| 171 | print("shmem_real_used.cdef shmem_real_used,1048576,/\n");
|
||
| 172 | } |
||
| 173 | |||
| 174 | |||
| 175 | sub test_service {
|
||
| 176 | system ("$MYSQLADMIN --version >/dev/null 2>/dev/null");
|
||
| 177 | if ($? == 0) |
||
| 178 | {
|
||
| 179 | system ("$COMMAND >/dev/null 2>/dev/null");
|
||
| 180 | if ($? == 0) |
||
| 181 | {
|
||
| 182 | print "yes\n"; |
||
| 183 | } |
||
| 184 | else |
||
| 185 | {
|
||
| 186 | print "no (could not connect to mysql)\n"; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | else |
||
| 190 | {
|
||
| 191 | print "no (mysqladmin not found)\n"; |
||
| 192 | } |
||
| 193 | exit 0; |
||
| 194 | } |
