Dashing: LabTech Agent Count via Meter Widget

This post needs some cleanup and perhaps more details. However, it should be ready to get you started using the “Meter” widget.

We’ve been fighting to get a grip on how all the widgets in the default Dashing project work. With the initial assistance of “dk01“, we were able to figure out how to get our data out of our LabTech MySQL database via a series of code in PHP. The PHP takes the data from MySQL and encodes it into JSON. The Dashing job (which is written in Ruby) takes in the JSON and processes it.

Today, I will share with you how we were able to get data from LabTech and into the Meter widget within Dashing thanks to the help of “DaveFP“.

We’re going to assume you have already followed the process outlined in another post to get Apache/PHP up and running on a server to query the MySQL database. If not, read here to get Dashing up and running and here to getting data ready for dashing.

Few Things Before We Begin

We are assuming the following

  • Your LabTech server IP is: 10.10.10.11
  • You have Apache/PHP running on your LabTech server via XAMPP on: 10.10.10.11
  • That Apache is listening on port 9080 via XAMPP
  • Your Dashing server IP is: 10.10.10.10

Add Widget to Dashboard

First, we need to put a widget in place on the actual dashboard. In this example, we’re putting it on the dashboards/sampletv.erb file.

Open the dashboards/sampletv.erb file in your favorite editor, and add the following code:

 HTML |  copy code |? 
1
    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
2
              <div data-id="agentcount" data-view="Meter" data-title="Agents" data-min="0" data-max="250" data-moreinfo="of 250 total agents."></div>
3
    </li>

Note 1: I have put in a “data-max” value of 250 – this is the current number of agents we have available in our LabTech server. Replace it with whatever your licensed number of LabTech agents count is.

Note 2: I have also put in the “250″ number inside the “data-moreinfo”. Again, update this number to reflect your total number of agents licensed.

Save the file.

You now have the widget placed on the dashboard. Next, let’s setup the code for grabbing our data from the MySQL database via PHP

Database Connection

The following code should be entered into a mysql.php file. This contains the code necessary for connecting to the LabTech server MySQL database via ODBC. In this case, our DSN is named “labtech” in the ODBC settings.

 PHP |  copy code |? 
01
<?php
02
 
03
try {
04
  $conn = new PDO('odbc:labtech;DRIVER={MySQL ODBC 3.51 Driver};DSN=labtech;');
05
}
06
catch (PDOException $e) {
07
  echo $e->getMessage();
08
}
09
 
10
?>

Query Database

Next, we need to create a PHP file that will handle the querying of MySQL and encoding of the results via JSON. Note, this file will be called from our Ruby job in a moment.

Place the following code into a PHP file called labtech-agents-total.php

 PHP |  copy code |? 
01
<?php
02
 
03
require 'mysql.php';
04
 
05
// We only get here if the connection succeeded
06
$sql = "SELECT count(*) as 'Agents' from labtech.computers c" ;
07
 
08
$stmt=$conn->prepare($sql);
09
 
10
$stmt->execute();
11
 
12
$results=$stmt->fetchAll(PDO::FETCH_ASSOC);
13
 
14
echo(json_encode($results,JSON_NUMERIC_CHECK))
15
 
16
?>

Here is a sample of the data that is presented once the PHP queries the database and encodes the information into JSON format:

1
[{"Agents":102}]

Create Job for Widget

Now, we need to create the job that will call our PHP file, and then take in the JSON results for processing. Place the following code into a Ruby file on the Dashing server called jobs/labtech-agent-count.rb

 Ruby |  copy code |? 
01
require 'net/http'
02
require 'json'
03
 
04
current_agentcount=0
05
last_agentcount=0
06
 
07
SCHEDULER.every '10m', :first_in => 0 do |job|
08
 
09
        last_agentcount = current_agentcount
10
 
11
        http = Net::HTTP.new('10.10.10.11',9080)
12
        response = http.request(Net::HTTP::Get.new("/labtech-agents-total.php"))
13
        results = JSON.parse(response.body)[0]
14
        if(results)
15
                current_agentcount = results["Agents"]
16
                send_event('agentcount', { value: current_agentcount})
17
        end
18
 
19
end

Test Dashboard

Now, it’s time to fire up your Dashing system and test out the dashboard to see if the widget is working.

Turn on the Dashing service using the following code from the server console:

 Bash |  copy code |? 
1
dashing start

Next, visit the Dashing URL to the sampletv.erb board. Go to http://10.10.10.10:3030/sampletv

You should see the following Meter on your dashboard.

Troubleshooting – Test Widget Directly

If you are unable to get the proper data to your widget from the Ruby job, you can test the system by using the “curl” command:

 Bash |  copy code |? 
1
curl -d '{ "auth_token": "YOUR_AUTH_TOKEN", "value": "123" }' http://10.10.10.10:3030/widgets/agentcount

This command will “push” the value of “123″ to the Agents Count meter  widget.

No comments yet.

Leave a Reply