Using groovy for scripting linux servers
Today I wanted to add some more graphs to our munin server monitoring. One graph should check the ftp backup server and plot how many of the available 100GB space is already used. Another graph should display the current sessions on out tomcat servers.
The munin documentation should how easy it is to create a plugin but I am not an advanced shell script programmer so I gave groovy a try. As I am a Java developer and I am using Grails in many of our latest projects groovy is a known environment for me.
I found some useful links that helped me doing the tasks I wanted to to with groovy in the munin plugins:
- Eric Wendelin’s blog post is a great introduction to using groovy for shell script
- this post on devilelephant helped me with piping output from one process to another
- Mytechtoday has a post about how to access environment variables in groovy
This simple groovy script allows me now to track active tomcat sessions via munin by calling the manager app:
#!/usr/bin/env groovy
def env = System.getenv()
def user = env['user'] ?: 'munin'
def password = env['password'] ?: 'munin'
def host = env['host'] ?: '127.0.0.1'
def port = env['ports'] ?: '8080'
def responsetext = "curl http://$user:$password@$host:$port/manager/list".execute().text
if (this.args.size() > 0 && this.args[0] == 'config') {
println 'graph_title Tomcat 2 sessions'
println 'graph_category tomcat'
responsetext.eachLine { line ->
def parts = line.split(':')
if (parts.size() > 1) {
println parts[3] + '.label ' + parts[3]
}
}
System.exit(1)
}
responsetext.eachLine { line ->
def parts = line.split(':')
if (parts.size() > 1) {
println parts[3] + '.value ' + parts[2]
}
}