Introduction
Using this DLL one can easily send data to observu monitor. To connect with observu monitor you need a JSON encoded set of records. Using this DLL you can send data easily by submitting the parameter. So basically you don't need to worry about JSON encoded set of records.
Background
a JSON encoded set of records that need to be added to the system. The records variable should look something like:
[
{"timestamp": 1234568, /* unix timestamp of the event */
"monitor_id":24324, /* only required if not implied by the API key */
"monitor_name":"my host", /* only required if not implied by the API key
and monitor_id is unknown, this requires the API
key to specify at least the service/group level
*/
"monitor_tags":{ /* not required */
"role": "web"
},
"data": { /* required */
'load:float': 4.52,
'disks.dev_sda.available:bytes': 523453,
'apache_running:boolean': true,
/*........ more vars */
},
/*................ more records .......... */
]
To send data we need to send a HTTP POST to: https://observu.com/api/add and the input parameter
key (Optional string):
an access key obtained from the observu website
source (Optional string)
a string that (uniquely) describes the current monitor
with this dll you can send sigle data,multiple data and can obtain new key and monitor id
Using the Code
Use of this DLL is simple. Just add the DLL to the project see here
...
How the DLL Created
Get a key automatically
public string[] addMonitorandGetKeyAndID(string username, string password, string servicename, string monitorname)
{
WebRequest request = WebRequest.Create("https://observu.com/api/add-external-monitor");
request.Method = "POST";
string postData = "username=" + username + "&password=" + password + "&service_name=" + servicename + "&monitor_name=" + monitorname + "";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
System.IO.Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
string[] r = responseFromServer.Split(',');
r = r[0].Split(':');
Console.WriteLine(r[1]);
string[] sendValue = new string[2];
if (r[1] != "\"ok\"")
{
string[] words = responseFromServer.Split(',');
string[] monitor_id = words[2].Split(':');
monitor_id = monitor_id[1].Split('\"');
words = words[1].Split(':');
words = words[1].Split('\"');
sendValue[0] = words[1];
sendValue[1] = monitor_id[1];
return sendValue;
}
return sendValue;
}
...
Send data to the observu server
public bool sendSingleData(string name,string type,Int64 value,string key,string monitorid)
{
string responseFromServer=null;
try
{
WebRequest request = WebRequest.Create(" https://observu.com/api/add");
request.Method = "POST";
Int64 timestamp = UnixTimestamp();
string datum = "\"data\":{\""+name+":"+type+"\""+":"+value.ToString()+"}";
string postData = "key=" + key + "&records=[{\"timestamp\":" + timestamp.ToString() + ", \"monitor_id\":" + monitorid + ", " + datum + "}]";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
}
catch (Exception e)
{
return false;
}
string[] r = responseFromServer.Split(',');
r = r[0].Split(':');
if (r[1] == "\"ok\"")
return true;
return false;
}
Reference
- https://observu.com/docs/api
- https://observu.com/docs/start