I've never been much of a python guy. Now there's this production environment where we've got a few basic things like bash, ruby (since we're using puppet) and python. Our Java based software has a nice JMX interface with a bunch of lifesavers in it and each JVM has the jolokia agent running (http/json access to jmx) so we can access this goodness fairly easily from scripts and/or the shell. So far what we'd be doing would be something like this:
$ curl http://server:port/jolokia/read/java.lang:type=OperatingSystem
This would return a ton of json and is very hard to read. Later we found out there's this niftly little python module and so we get nicely (and readable) json output by just piping it trough python:
$ curl http://server:port/jolokia/read/java.lang:type=OperatingSystem | python -m json.tool
So far so good. Now because I'm lazy and don't like typing or even copying these urls into a shell, a bash script was born, which was a collection of calls like you see above. You'd source the script and use the functions contained in it like they were system commands. Pretty nice, I thought. Until we ran into a problem that needed checking if something had a certain state and then changing that state, thus actually working with json in a bash -- good luck with that!
Since we've already used that nifty json.tool I ventured into using python for this and it turned out it's pretty damn awesome. We can now call a jolokia url, work with the return value and do some more calls based on that. I did that using the subprocess module and calling the shell functions but that's ugly. So I went ahead and ported the shell script to python.
What you see below is a python script that can be used either as a module or directly from the commandline. Thanks to python's cool inspection/reflection capabilities (As a java guy I'm used to that anyway), you can extend this script by simply adding another url and correspondingly named function. If something's off it tries to print an error as good as possible (includes argument names, how cool is that!) and if everything's right it just works as you'd expect.
I hope there are other jolokia users out there who will find this useful. And to the python wizards: Let me know if and how I can improve on this script (especially the main function).
So yeah, I've never been much of a python guy -- until now. Enjoy my first python script!
Post a Comment