Introduction
Let’s say I have a web application and I want to know how many users are online. My application has a rest API from which I can get this number in json. So I need to somehow get it and pass to machine agent. The response I got is like this:
Type of extension
AppDynamics supports three types of extensions:
- Using a script
- Using java application
- Using HTTP listener
Basically the idea of first two is when they run machine agent reads the output of them (which is in a special format) and sends it to the controller. Since writing a script is simpler in terms of support and future extension than java application I decided to use a script way. The idea is to create a ruby script which will be called by .sh script. At the end the extension will consist of three files:
- rest-monitor.sh
- rest-monitor.rb
- monitor.xml
Ruby script
The script’s task is to call HTTP service which in my case returns response in json, parse it and write to console formatted metric. Metric format should be as following (example from default extension):
And here is the method which formats the metric to such format:
Now we need to call a service, in my case it is HTTPS with basic authentication. In ruby it’s quite easy to perform such call:
res.body
will contain json response, but first we need to parse it, get value by key and print it:
The whole script is:
Please note that I was using ruby version 1.8.7, so that’s why I needed to include explicitly net/https
and rubygems
. If you have newer version (ruby -v
to check it) you probably won’t need this.
Shell script
Since the logic is in ruby shell script is just a wrapper for it, so it should just run ruby script:
Monitor.xml
This file is almost the same for every monitor, it basically tells machine agent how to execute the script. You can find more details in step 7 of original documentation. For my monitor it looks like this:
Now you just need to put these files under monitors folder and restart machine agent. Metrics will be available under Custom Metrics|Users|online