I was inspired by this blog to create a real-time dashboard to monitor my laptop Windows 8.1 CPU usage in the SAP HANA Cloud Platform. We will create a simple app in the HANA cloud platform with WebSocket, some Python and VBScript scripts to get the CPU usage and display it in a simple gauge.
Prerequisites
The app will look like this:
Create a Web Project in Eclipse
package rtd;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/rtd")
public class RTD {
private static final Set<Session> peers = Collections
.synchronizedSet(new HashSet<Session>());
@OnMessage
public void onMessage(final String message) {
for (final Session peer : peers) {
try {
peer.getBasicRemote().sendText(message);
} catch (final IOException e) {
e.printStackTrace();
}
}
}
@OnOpen
public void onOpen(final Session peer) {
peers.add(peer);
}
@OnClose
public void onClose(final Session peer) {
peers.remove(peer);
}
}
<script>
//var wsUri = "ws://localhost:8080/rtd/rtd";
var wsUri = "wss://rtdp057134trial.hanatrial.ondemand.com/rtd/rtd";
function init() {
output = document.getElementById("output");
}
function send_message() {
websocket = new WebSocket(wsUri);
websocket.onopen = function(evt) {
onOpen(evt)
};
websocket.onmessage = function(evt) {
onMessage(evt)
};
websocket.onerror = function(evt) {
onError(evt)
};
}
function onOpen(evt) {
console.log("Connected to rtd!");
doSend(0);
}
function onMessage(evt) {
console.log(evt.data);
updateGauges(evt.data)
}
function onError(evt) {
console.log('Error: ' + evt.data);
}
function doSend(message) {
websocket.send(message);
//websocket.close();
}
window.addEventListener("load", init, false);
var gauges = [];
function createGauge(name, label, min, max)
{
var config =
{
size: 300,
label: label,
min: undefined != min ? min : 0,
max: undefined != max ? max : 100,
minorTicks: 5
}
var range = config.max - config.min;
config.greenZones = [{ from: config.min, to: config.min + range*0.75 }];
config.yellowZones = [{ from: config.min + range*0.75, to: config.min + range*0.9 }];
config.redZones = [{ from: config.min + range*0.9, to: config.max }];
gauges[name] = new Gauge(name + "GaugeContainer", config);
gauges[name].render();
}
function createGauges()
{
createGauge("cpu", "CPU Usage Monitoring");
}
function updateGauges(value)
{
for (var key in gauges)
{
console.log(value);
gauges[key].redraw(value);
}
}
function initialize()
{
createGauges();
send_message();
}
</script>
Deploy to HANA Cloud Platform
After you have created the web project in Eclipse, we will deploy the app in the HANA Cloud Platform.
Send Data to WebSocket
We are going to use the VBScript to get the CPU usage and send the information to WebSocket using Python. Since we are deploying the app in the HANA Cloud platform, the WebSocket URI is:
wss://rtdp057134trial.hanatrial.ondemand.com/rtd/rtd
If you would like to test in the HANA local runtime, change the URI to:
ws://localhost:/8080/rtd/rtd
Below is the snippet Python program to send the information to the WebSocket:
#!/usr/bin/python
import sys
from websocket import create_connection
ws = create_connection("wss://rtdp057134trial.hanatrial.ondemand.com/rtd/rtd")
#ws = create_connection("ws://localhost:8080/rtd/rtd")
arg1 = str(sys.argv[1])
print (arg1)
ws.send(arg1)
result = ws.recv()
ws.close()
Open your command prompt and type the following: cscript //nologo getCPUUsage.vbs
If there is no error, you will see the movement of the pointer in the gauge.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
6 | |
5 | |
5 | |
5 | |
5 | |
4 | |
4 | |
4 | |
3 | |
3 |