MySQL Enterprise Monitor – send advisor events to your chat client with Perl and Jabber
December 30, 2013 Leave a comment
MySQL Enterprise Monitor (MEM) is part of the MySQL Enterprise Edition, and MEM provides real-time visibility into the performance and availability of all your MySQL databases. MEM and the MySQL Query Analyzer continuously monitor your databases and alerts you to potential problems before they impact your system. It’s like having a “Virtual DBA Assistant” at your side to recommend best practices to eliminate security vulnerabilities, improve replication, optimize performance and more. As a result, the productivity of your developers, DBAs and System Administrators is improved significantly.
With MEM, you have a couple of notification options for receiving information when MEM has received an event alert. An event alert is a “significant deviation from the baseline performance trends, and best-practice Advisors recommended changes to configuration and variable settings to improve performance”. From: http://www.mysql.com/products/enterprise/monitor.html
You may choose to receive these alerts via email or SNMP. I am not going to go over how to setup these alerts – but instructions may be found here.
I have MEM installed on my home server, and since I have been using it for a while, I have been able to tune my instances and databases so that I rarely receive any alerts. I normally receive these alerts via email, but I don’t always check email for this particular account during the day. Since most of my colleagues are in other parts of the world, we use a Jabber chat client for quick communications during the day. I wanted to figure out a way for me to receive a chat message whenever MEM had an alert. For my chat client, I use adium – which is an open-source multi-protocol instant messaging client for Mac OS X – it supports MSN, Jabber, Yahoo! and other networks. But this should work with any XMPP-based chat client application.
You will probably want to create a new POP3 email address for these alerts, as this script will delete the email messages from the server. If you want to keep an email copy of these alerts, you may add an additional email address to the alert notification group. If you use a free email service (like gmail), remember that it has a daily limit of the number of messages that you can send. I ran into this problem when testing the scripts, so I created my own email under one of my domains. I found two Perl scripts; one that acts as a POP3 client, and another that sends the Jabber message – and I combined them into one Perl script. You might need to do a little tweaking to get it to work with your service.
This is a sample email message from MEM. The first “Subject” line is the actual email subject. In MEM, you can customize what information you want in your email subject line via Email Notification Groups. I combined several parts of the email message into one message to be sent via chat.
(Email Subject Line) (macserver01, MacServer01) - MEM WARNING Alert: User Has Rights To Database That Does Not Exist (Email Body) Subject: macserver01, MacServer01 Time: 2013-12-30 17:01:44 UTC (2013-12-30 12:01:44 EST) Category: Security Event: User Has Rights To Database That Does Not Exist Problem Description When a database is dropped, user privileges on the database are not automatically dropped. This has security implications as that user will regain privileges if a database with the same name is created in the future, which may not be the intended result. Advice Revoke privileges for all users on any databases that do not exist. The following users have privileges on databases that do not exist. ''@'%' on database test_% Recommended Action REVOKE ALL PRIVILEGES ON db_name.* FROM 'user_name'@'host_name'; Links and Further Reading MySQL Manual: DROP DATABASE Syntax MySQL Manual: GRANT Syntax MySQL Manual: REVOKE Syntax MySQL Manual: Privileges Provided by MySQL MySQL Manual: How the Privilege System Works Securing Your MySQL Installation Securing a MySQL Server on Windows Expression %user% != THRESHOLD Evaluated Expression ''@'%' on database test_% != '' Copyright © 2005, 2013, Oracle and/or its affiliates. All rights reserved.
And here is the script. You may run it as a cron job, where it will check your email every few minutes and then send you a chat message when an email with an alert has arrived. You will need to modify the script to match your email and chat settings, and I have placed notes in the script to help guide you:
#!/usr/bin/perl -w # POP3 client script source: # http://forums.devshed.com/perl-programming-6/how-to-get-gmail-mail-by-mail-pop3client-in-perl-555889.html # Author: a user named keath # Jabber message script source: # ttp://dipinkrishna.com/blog/2010/12/perl-send-chat-message-gmail-buddy-jabber/ # Author: Dipin Krishna use strict; # for the email use Mail::POP3Client; # for the chat use Net::Jabber; # I was having a problem with the Debug module, so I just # commented line 154 in /Library/Perl/5.16/Net/XMPP/Debug.pm # this is the email address that you want to use to receive # the alert messages from MySQL Enterprise Monitor my $user = 'MEMalerts@scriptingmysql.com'; my $pass = 'mypassword'; # you will need to use your POP3 server name my $host = "pop.emailserver.com"; my $pop = new Mail::POP3Client( USER => $user, PASSWORD => $pass, HOST => $host, PORT => 995, USESSL => 'true', ); # I have commented most of the print messages # - you may uncomment them as you wish my $count = $pop->Count(); if ($count Message(); } elsif ($count == 0) { print "no messages\n"; } else { #print "$count messsages\n\n"; for my $i (1 .. $count) { my $subject = ""; my $message = ""; # if you want to extract data from the head of the email # I am extracting data from the body #foreach ($pop->Head($i)) { foreach ($pop->Body($i)) { #print "$_\n" if /^(From|Subject|Date):/i; #print "$_\n" if /^(Subject|Date):/i; # my $message = "$_\n" if /^(Subject):/i; # I am building my message so that it contains the information in this order: # Category, Subject, Event if ($_ =~ "^Subject") { #print "$_\n"; chomp $_; $subject = $_; $subject =~ s/Subject: //; } if ($_ =~ "^Category") { #print "$_\n"; chomp $_; $message = "$_ || $subject"; $message =~ s/Category: //; } if ($_ =~ "^Event") { #print "$_\n"; chomp $_; $message = "$message || $_"; $message =~ s/Event: //; my $sttime=time; #print "Message: $_\n"; # this is my Google Talk chat user name and password my $username = 'mem.scripting.mysql';; my $password = 'mypassword'; my $to = 'my_email_address'; my $msg = "$message"; #print "$to: $msg\n"; my $resource = "dipin"; my $hostname = 'talk.google.com'; my $port = 5222; my $componentname = 'gmail.com'; my $Contype = 'tcpip'; my $tls = 1; my $Con = new Net::Jabber::Client(); $Con->SetCallBacks(presence=>\&presence, message=>\&message ); my $status = $Con->Connect( hostname => $hostname, port => $port, componentname => $componentname, connectiontype => $Contype, tls => $tls); if (!(defined($status))) { print "ERROR: XMPP connection failed.\n"; print " ($!)\n"; exit(0); } # Change hostname my $sid = $Con->{SESSION}->{id}; $Con->{STREAM}->{SIDS}->{$sid}->{hostname} = $componentname; # Authenticate #my @result = $Con->AuthSend( my @result = $Con->AuthIQAuth( username => $username, password => $password, resource => $resource); #print "Result: $result[0] $result[1]\n"; if ($result[0] ne "ok") { print "ERROR: Authorization failed: $result[0] - $result[1]\n"; } else { #print "Logged in Sucessfull!\n"; $Con->PresenceSend(show=>"Available"); #print "Sending Message!\n"; $Con->MessageSend(to=>"$to", subject=>"Test", body=>"$msg\n", priority=>10); } # END send Jabbar message # # # # # # # # # # # # # # # # # # # # # # # # # # # # this deletes the message from the server $pop->Delete( $i ); # if you only want to send one email message as a test, # uncomment this line exit; } } print "\n"; } } $pop->Close(); exit;
![]() |
Tony Darnell is a Principal Sales Consultant for MySQL, a division of Oracle, Inc. MySQL is the world’s most popular open-source database program. Tony may be reached at info [at] ScriptingMySQL.com and on LinkedIn. |
![]() |
Tony is the author of Twenty Forty-Four: The League of Patriots
Visit http://2044thebook.com for more information. |