Skip to content

Improve structure #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
BasedOnStyle: Mozilla
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
IndentWidth: 4
ColumnLimit: 180
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AllowShortFunctionsOnASingleLine: false
AlignAfterOpenBracket: AlwaysBreak
AllowAllArgumentsOnNextLine: false
FixNamespaceComments: true
PenaltyBreakAssignment: 1000
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
# BeforeLambdaBody: false
# BeforeWhile: true
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
14 changes: 1 addition & 13 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
# Compiled Object files
*.slo
*.lo
*.o

# Compiled Dynamic libraries
*.so
*.dylib

# Compiled Static libraries
*.lai
*.la
*.a
/build
22 changes: 5 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
cmake_minimum_required (VERSION 3.2)
project (StatsdClient)
cmake_minimum_required(VERSION 3.2)

set (StatsdClient_VERSION_MAJOR 1)
set (StatsdClient_VERSION_MINOR 1)
project(StatsdClient LANGUAGES CXX)

include_directories ("src")
add_subdirectory ("src")
add_library(StatsdClient STATIC src/statsd_client.cpp)

configure_file (
"src/StatsdClientConfig.h.in"
"StatsdClientConfig.h"
)

set_property (SOURCE "StatsdClientConfig.h" PROPERTY GENERATED TRUE)

target_compile_features(StatsdClient PRIVATE cxx_nullptr)

install (FILES "${PROJECT_BINARY_DIR}/StatsdClientConfig.h"
DESTINATION include)
target_include_directories(StatsdClient PUBLIC inc)

add_subdirectory(demo)
11 changes: 0 additions & 11 deletions Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
2. Next , build the generated `Visual Studio` solution. `StatsDClient.lib` will be built.

## API
See [header file](src/statsd_client.h) for more api detail.
See [header file](inc/statsd_client.h) for more api detail.

** Notice: this client is not thread-safe **

Expand Down
5 changes: 5 additions & 0 deletions demo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_executable(system_monitor system_monitor.cpp)
target_link_libraries(system_monitor StatsdClient)

add_executable(test_client test_client.cpp)
target_link_libraries(test_client StatsdClient)
19 changes: 0 additions & 19 deletions demo/Makefile

This file was deleted.

113 changes: 61 additions & 52 deletions demo/system_monitor.cpp
Original file line number Diff line number Diff line change
@@ -1,100 +1,102 @@
#include <statsd_client.h>

#include <sys/types.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <unistd.h>

#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <netinet/in.h>
#include <string>
#include <sys/ioctl.h>
#include <vector>

#include "statsd_client.h"

using namespace std;

static int running = 1;

void sigterm(int sig)
{
running = 0;
}

string localhost() {
std::string localhost()
{
struct addrinfo hints, *info, *p;
string hostname(1024, '\0');
std::string hostname(1024, '\0');
gethostname((char*)hostname.data(), hostname.capacity());

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; /*either IPV4 or IPV6*/
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_CANONNAME;

if ( getaddrinfo(hostname.c_str(), "http", &hints, &info) == 0) {
for(p = info; p != NULL; p = p->ai_next) {
if (getaddrinfo(hostname.c_str(), "http", &hints, &info) == 0)
{
for (p = info; p != NULL; p = p->ai_next)
{
hostname = p->ai_canonname;
}
freeaddrinfo(info);
}

string::size_type pos = hostname.find(".");
while ( pos != string::npos )
std::string::size_type pos = hostname.find(".");
while (pos != std::string::npos)
{
hostname[pos] = '_';
pos = hostname.find(".", pos);
}
return hostname;
}

vector<string>& StringSplitTrim(const string& sData,
const string& sDelim, vector<string>& vItems)
std::vector<std::string>& StringSplitTrim(const std::string& sData, const std::string& sDelim, std::vector<std::string>& vItems)
{
vItems.clear();

string::size_type bpos = 0;
string::size_type epos = 0;
string::size_type nlen = sDelim.size();
std::string::size_type bpos = 0;
std::string::size_type epos = 0;
std::string::size_type nlen = sDelim.size();

while(sData.substr(epos,nlen) == sDelim)
while (sData.substr(epos, nlen) == sDelim)
{
epos += nlen;
}
bpos = epos;

while ((epos=sData.find(sDelim, epos)) != string::npos)
while ((epos = sData.find(sDelim, epos)) != std::string::npos)
{
vItems.push_back(sData.substr(bpos, epos-bpos));
vItems.push_back(sData.substr(bpos, epos - bpos));
epos += nlen;
while(sData.substr(epos,nlen) == sDelim)
while (sData.substr(epos, nlen) == sDelim)
{
epos += nlen;
}
bpos = epos;
}

if(bpos != sData.size())
if (bpos != sData.size())
{
vItems.push_back(sData.substr(bpos, sData.size()-bpos));
vItems.push_back(sData.substr(bpos, sData.size() - bpos));
}
return vItems;
}

int main(int argc, char *argv[])
int main(int argc, char* argv[])
{
FILE *net, *stat;
struct sysinfo si;
char line[256];
unsigned int user, nice, sys, idle, total, busy, old_total=0, old_busy=0;
unsigned int user, nice, sys, idle, total, busy, old_total = 0, old_busy = 0;

if (argc != 3) {
printf( "Usage: %s host port\n"
"Eg: %s 127.0.0.1 8125\n",
argv[0], argv[0]);
if (argc != 3)
{
printf(
"Usage: %s host port\n"
"Eg: %s 127.0.0.1 8125\n",
argv[0],
argv[0]);
exit(1);
}

Expand All @@ -103,43 +105,50 @@ int main(int argc, char *argv[])
signal(SIGCHLD, SIG_IGN); /* will save one syscall per sleep */
signal(SIGTERM, sigterm);

if ( (net = fopen("/proc/net/dev", "r")) == NULL) {
if ((net = fopen("/proc/net/dev", "r")) == NULL)
{
perror("fopen");
exit(-1);
}

if ( (stat = fopen("/proc/stat", "r")) == NULL) {
if ((stat = fopen("/proc/stat", "r")) == NULL)
{
perror("fopen");
exit(-1);
}

string ns = string("host.") + localhost().c_str() + ".";
std::string ns = std::string("host.") + localhost().c_str() + ".";
statsd::StatsdClient client(argv[1], atoi(argv[2]), ns);

daemon(0,0);
daemon(0, 0);
printf("running in background.\n");

while(running) {
while (running)
{
rewind(net);
vector<string> items;
while(!feof(net)) {
std::vector<std::string> items;
while (!feof(net))
{
fgets(line, sizeof(line), net);
StringSplitTrim(line, " ", items);

if ( items.size() < 17 ) continue;
if ( items[0].find(":") == string::npos ) continue;
if ( items[1] == "0" and items[9] == "0" ) continue;

string netface = "network."+items[0].erase( items[0].find(":") );
client.count( netface+".receive.bytes", atoll(items[1].c_str()) );
client.count( netface+".receive.packets", atoll(items[2].c_str()) );
client.count( netface+".transmit.bytes", atoll(items[9].c_str()) );
client.count( netface+".transmit.packets", atoll(items[10].c_str()) );
if (items.size() < 17)
continue;
if (items[0].find(":") == std::string::npos)
continue;
if (items[1] == "0" and items[9] == "0")
continue;

std::string netface = "network." + items[0].erase(items[0].find(":"));
client.count(netface + ".receive.bytes", atoll(items[1].c_str()));
client.count(netface + ".receive.packets", atoll(items[2].c_str()));
client.count(netface + ".transmit.bytes", atoll(items[9].c_str()));
client.count(netface + ".transmit.packets", atoll(items[10].c_str()));
}

sysinfo(&si);
client.gauge("system.load", 100*si.loads[0]/0x10000);
client.gauge("system.freemem", si.freeram/1024);
client.gauge("system.load", 100 * si.loads[0] / 0x10000);
client.gauge("system.freemem", si.freeram / 1024);
client.gauge("system.procs", si.procs);
client.count("system.uptime", si.uptime);

Expand All @@ -150,7 +159,7 @@ int main(int argc, char *argv[])
total = user + sys + idle;
busy = user + sys;

client.send("system.cpu", 100 * (busy - old_busy)/(total - old_total), "g", 1.0);
// client.send("system.cpu", 100 * (busy - old_busy) / (total - old_total), "g", 1.0);

old_total = total;
old_busy = busy;
Expand Down
14 changes: 9 additions & 5 deletions demo/test_client.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
#include <statsd_client.h>

#include <iostream>
#include <unistd.h>
#include "statsd_client.h"

int main(void)
{
std::cout << "running..." << std::endl;

statsd::StatsdClient client;
statsd::StatsdClient client2("127.0.0.1", 8125, "myproject.abx.");
statsd::StatsdClient client2{ "127.0.0.1", 8125, "myproject.abx." };

client.event({ "An error occurred", "Error message", statsd::Event::Type::error, { "env:dev" } });

client.count("count1", 123, 1.0);
client.count("count2", 125, 1.0);
client.gauge("speed", 10);
client2.timing("request", 2400);

sleep(1);

client.inc("count1", 1.0);
client2.dec("count2", 1.0);
int i;
for(i=0; i<10; i++) {

for (int i = 0; i < 10; i++)
{
client2.count("count3", i, 0.8);
}

std::cout << "done" << std::endl;
return 0;
}
Loading