Commit 1a86b131 authored by Christopher Reis's avatar Christopher Reis

intitial commit

parent d97cef69
SRCS = gm_system_data.cpp
TARGET = gm_system_data
include ../AppMake.mf
/*
* Copyright 2007-2016 United States Government as represented by the
* Administrator of The National Aeronautics and Space Administration.
* No copyright is claimed in the United States under Title 17, U.S. Code.
* All Rights Reserved.
*/
/**
* @file gmpub.cpp
*
* A C++ example demonstration of GMSEC publish/subscribe functionality.
* The associated example gmsub.cpp will listen for this publish program.
* gmsub should be run before gmpub.
*
*/
#include "../example.h"
//#include "PublishThread.hpp"
//#include "SubscribeThread.hpp"
#include <gmsec4/util/StdSharedPtr.h>
#include <gmsec4/util/StdThread.h>
#include <memory>
#include <string>
#include <unistd.h>
#include <algorithm>
#include "system_data.hpp"
using namespace gmsec::api;
using namespace gmsec::api::util;
char hostname[256];
std::string default_subject = "GMSEC.SYSDATA.";
class gmpub
{
public:
gmpub(Config& config);
~gmpub();
bool run();
private:
Config& config;
Connection* connection;
void publish(int iteration, const std::string& subject);
};
gmpub::gmpub(Config &c)
: config(c),
connection(0)
{
/* Initialize config */
example::initialize(c);
gethostname(hostname, 256);
std::string strhost = hostname;
default_subject += strhost;
transform(default_subject.begin(), default_subject.end(), default_subject.begin(),::toupper);
}
gmpub::~gmpub()
{
if (connection)
{
connection->disconnect();
Connection::destroy(connection);
}
Connection::shutdownAllMiddlewares();
}
bool gmpub::run()
{
bool success = true;
/* output GMSEC API version */
GMSEC_INFO << Connection::getAPIVersion();
try
{
//o Create the Connection
connection = Connection::create(config);
//o Connect
connection->connect();
//o Get information from the command line
std::string subject = example::get(config, "SUBJECT", default_subject);
int iterations = example::get(config, "ITERATIONS", 1);
int interval_ms = example::get(config, "INTERVAL_MS", 1000);
//o Output middleware version
GMSEC_INFO << "Middleware version = " << connection->getLibraryVersion();
//o Output information
GMSEC_INFO << "Using subject '" << subject.c_str() << "'";
GMSEC_INFO << "Publishing " << iterations << " message(s)";
if (iterations > 1)
{
GMSEC_INFO << "Publish interval " << interval_ms << " [ms]";
}
//o Publish message(s)
for (int i = 0; i < iterations; ++i)
{
publish(i, subject);
if (i < iterations - 1)
{
TimeUtil::millisleep(interval_ms);
}
}
}
catch (Exception& e)
{
GMSEC_ERROR << e.what();
success = false;
}
return success;
}
void gmpub::publish(int iteration, const std::string& subject)
{
int value = iteration + 1;
GMSEC_INFO << "working";
//o Create message
Message message(subject.c_str(), Message::PUBLISH);
proc_data sysinfo;
getSystemData(&sysinfo);
//Add fields to message
message.addField("SYS-UPTIME", (GMSEC_U64) sysinfo.uptime);
message.addField("SYS-IDLETIME", (GMSEC_U64) sysinfo.idle);
message.addField("SYS-CPU-PROC", (GMSEC_F32) sysinfo.proc_percent);
message.addField("SYS-MEMFREE",(GMSEC_U64) sysinfo.memfree);
message.addField("SYS-MEMTOTAL",(GMSEC_U64) sysinfo.memtotal);
message.addField("SYS-MEMACTIVE",(GMSEC_U64) sysinfo.memactive);
message.addField("SYS-MEMAVAILABLE",(GMSEC_U64) sysinfo.memavailable);
message.addField("SYS-MEMUSED",(GMSEC_U64) sysinfo.memused);
message.addField("SYS-RXBYTES",(GMSEC_U64) sysinfo.rxbytes);
message.addField("SYS-RXPACKETS",(GMSEC_U64) sysinfo.rxpackets);
message.addField("SYS-TXBYTES",(GMSEC_U64) sysinfo.txbytes);
message.addField("SYS-TXPACKETS",(GMSEC_U64) sysinfo.txpackets);
// message.addField("CHAR-FIELD", (GMSEC_CHAR) 'c');
// message.addField("BOOL-FIELD-TRUE", true);
// message.addField("BOOL-FIELD-FALSE", false);
// message.addField("I8-FIELD", (GMSEC_I8) value);
// message.addField("I16-FIELD", (GMSEC_I16) value);
// message.addField("I32-FIELD", (GMSEC_I32) value);
// message.addField("I64-FIELD", (GMSEC_I64) value);
// message.addField("U8-FIELD", (GMSEC_U8) value);
// message.addField("U16-FIELD", (GMSEC_U16) value);
// message.addField("U32-FIELD", (GMSEC_U32) value);
// message.addField("U64-FIELD", (GMSEC_U64) value);
// message.addField("COUNT", (GMSEC_I32) iteration);
// message.addField("STRING-FIELD", "This is a test");
// message.addField("F32-FIELD", GMSEC_F32(1 + 1. / value));
// message.addField("F64-FIELD", GMSEC_F64(1 + 1. / value));
// message.addField("BIN-FIELD", (GMSEC_BIN) "JLMNOPQ", 7);
if (config.getBooleanValue("ENCRYPT", false))
{
message.addField("SEC-ENCRYPT", true);
}
//o Publish Message
connection->publish(message);
//o Display XML representation of message
GMSEC_INFO << "Published:\n" << message.toXML();
}
int main(int argc, char* argv[])
{
init();
Config config(argc, argv);
example::addToConfigFromFile(config);
if (example::isOptionInvalid(config, argc))
{
example::printUsage("gmpub");
return -1;
}
gmpub(config).run();
}
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include "../example.h"
using namespace gmsec::api;
using namespace gmsec::api::util;
struct proc_data {
double uptime;
double idle;
double proc_percent;
long memfree;
long memtotal;
long memavailable;
long memactive;
long meminactive;
long memused;
long memswap;
long memswapfree;
long rxbytes;
long rxpackets;
long txbytes;
long txpackets;
};
bool network_init(true);
static unsigned long long lastTotalUser, lastTotalUserNice, lastTotalSys, lastTotalIdle;
static unsigned long lastRXBytes, lastRXPackets, lastTXBytes, lastTXPackets;
int wait_millis(int ms){
TimeUtil::millisleep(ms);
return 0;
}
/***
For setting up initial variables used in calculations
***/
void init(){
FILE* file = fopen("/proc/stat", "r");
fscanf(file, "cpu %llu %llu %llu %llu", &lastTotalUser, &lastTotalUserNice,
&lastTotalSys, &lastTotalIdle);
fclose(file);
wait_millis(100);
}
void getSystemData(proc_data *pd){
FILE* file;
//Uptime and Idle
file = fopen("/proc/uptime", "r");
fscanf(file, "%lf %lf", &pd->uptime, &pd->idle);
fclose(file);
//Percent Utilization
unsigned long long user, nice, sys, idle, total;
double percent;
file = fopen("/proc/stat", "r");
fscanf(file, "cpu %llu %llu %llu %llu", &user, &nice,&sys, &idle);
fclose(file);
//Calculate Percent Utilization
total = (user - lastTotalUser)+(nice - lastTotalUserNice)+(sys - lastTotalSys)+(idle - lastTotalIdle);
if (user < lastTotalUser || nice < lastTotalUserNice || sys < lastTotalSys || idle < lastTotalIdle){
percent = -1.0;
} else {
percent = ((user - lastTotalUser)+(nice - lastTotalUserNice)+(sys - lastTotalSys));
percent /= total;
percent *= 100;
}
pd->proc_percent = percent;
lastTotalIdle = idle;
lastTotalSys = sys;
lastTotalUser = user;
lastTotalUserNice = nice;
//memory
char buff[100];
unsigned long ul_MemTotal, ul_MemFree, ul_MemAvailable, ul_MemActive, ul_MemInactive, ul_MemSwap, ul_MemSwapFree;
file = fopen("/proc/meminfo", "r");
while (fgets(buff,75,file) != NULL) {
if (strstr(buff,"MemTotal:")) {
sscanf(buff, "MemTotal: %lu kB", &ul_MemTotal);
} else if (strstr(buff,"MemFree:")) {
sscanf(buff, "MemFree: %lu kB", &ul_MemFree);
} else if (strstr(buff,"MemAvailable:")) {
sscanf(buff, "MemAvailable: %lu kB", &ul_MemAvailable);
} else if (strstr(buff,"Active:")) {
sscanf(buff, "Active: %lu kB", &ul_MemActive);
} else if (strstr(buff,"SwapTotal:")) {
sscanf(buff, "SwapTotal: %lu kB", &ul_MemSwap);
} else if (strstr(buff,"SwapFree:")) {
sscanf(buff, "SwapFree: %lu kB", &ul_MemSwapFree);
} else if (strstr(buff,"MemInactive:")) {
sscanf(buff, "MemInactive: %lu kB", &ul_MemInactive);
}
}
fclose(file);
pd->memfree = ul_MemFree;
pd->memtotal = ul_MemTotal;
pd->memused = ul_MemTotal - ul_MemFree;
pd->memavailable = ul_MemAvailable;
pd->memactive = ul_MemActive;
pd->meminactive = ul_MemInactive;
pd->memswap = ul_MemSwap;
pd->memswapfree = ul_MemSwapFree;
//Network Data
unsigned long ul_TXBytes, ul_TXPackets,ul_RXBytes, ul_RXPackets;
file = fopen("/proc/net/dev", "r");
while (fgets(buff,100,file) != NULL) {
if (strstr(buff,"enp0s3:")) {
//std::cout << buff << '\n';
sscanf(buff, "enp0s3: %lu %lu %*u %*u %*u %*u %*u %*u %lu %lu", &ul_RXBytes, &ul_RXPackets, &ul_TXBytes, &ul_TXPackets);
}
}
fclose(file);
//std::cout << ul_RXBytes << " " << ul_RXPackets << " " << ul_TXBytes << " " << ul_TXPackets << '\n';
if(network_init){
lastRXBytes = ul_RXBytes;
lastRXPackets = ul_RXPackets;
lastTXBytes = ul_TXBytes;
lastTXPackets = ul_TXPackets;
network_init = false;
}
//return network data
pd->rxbytes=ul_RXBytes - lastRXBytes;
pd->rxpackets=ul_RXPackets - lastRXPackets;
pd->txbytes=ul_TXBytes - lastTXBytes;
pd->txpackets=ul_TXPackets - lastTXPackets;
//update variables
lastRXBytes = ul_RXBytes;
lastRXPackets = ul_RXPackets;
lastTXBytes = ul_TXBytes;
lastTXPackets = ul_TXPackets;
}
// int main(int argc, char const *argv[]) {
// proc_data sysinfo;
// init();
// for (size_t i = 0; i < 50; i++) {
// getSystemData(&sysinfo);
// std::cout << "Uptime " << sysinfo.uptime << " " << "Utilization " << sysinfo.proc_percent << '\n';
// wait_millis(2000);
// }
//
// return 0;
// }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment