-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrdtool.cpp
186 lines (162 loc) · 4.51 KB
/
rrdtool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
** NetXMS - Network Management System
** Performance Data Storage Driver for RRDTools
** Copyright (C) 2014 Raden Solutions
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
** File: rrd.cpp
**/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <nms_core.h>
#include <pdsdrv.h>
/**
* Driver class definition
*/
class RRDToolStorageDriver : public PerfDataStorageDriver
{
private:
static const int PORTNO = 2003;
static char const *HOSTNAME;
static int sockfd;
virtual void error(const char *msg);
public:
virtual const TCHAR *getName();
virtual bool saveDCItemValue(DCItem *dcObject, time_t timestamp, const TCHAR *value);
virtual bool saveDCTableValue(DCTable *dcObject, time_t timestamp, Table *value);
virtual bool init();
virtual void shutdown();
};
int RRDToolStorageDriver::sockfd = 0;
const char *RRDToolStorageDriver::HOSTNAME = "graphite.ctco.lv";
/**
* Driver name
*/
static TCHAR *s_driverName = _T("RRDTOOL");
/**
* Get name
*/
const TCHAR *RRDToolStorageDriver::getName()
{
return s_driverName;
}
const TCHAR *reverseFQDN(const TCHAR *fqdn)
{
char pch[512];
char result[512];
char value[512];
char const dot = '.';
int index, start, end;
strcpy(value, fqdn);
start = 0;
end = strlen(fqdn)-1;
bzero(result,sizeof(result));
for (index = strlen(value)-1; index >= 0; index--)
{
if (value[index] == dot)
{
memcpy(result + start, value + index + 1, end - index);
end = index - 1;
start = strlen(fqdn) - index;
memcpy(result + start - 1, &dot, 1);
}
}
memcpy(result + start, value, end + 1);
return result;
}
bool RRDToolStorageDriver::init()
{
struct sockaddr_in serv_addr;
struct hostent *server;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(HOSTNAME);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(PORTNO);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
return true;
}
void RRDToolStorageDriver::shutdown()
{
close(sockfd);
}
/**
* Save DCI value
*/
bool RRDToolStorageDriver::saveDCItemValue(DCItem *dcObject, time_t timestamp, const TCHAR *value)
{
char buffer[256];
const char * hostname = dcObject->getOwner()->getName();
char timestr[32];
char inverseHostname[256];
int n;
time_t timer;
bzero(buffer,256);
bzero(timestr, 32);
// bzero(hostname,256);
// hostname = dcObject->getOwner()->getName();
// inverseHostname = reverseFQDN(hostname);
timer = time(NULL);
strcat(buffer, reverseFQDN(hostname));
strcat(buffer, ".");
strcat(buffer, dcObject->getDescription());
strcat(buffer, " ");
strcat(buffer, value);
strcat(buffer, " ");
sprintf(timestr, "%u", timestamp);
strcat(buffer, timestr);
// perror(buffer);
strcat(buffer, "\n");
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
// _tprintf(_T("SAVE: %s %s\n\n"), dcObject->getName(), value);
return true;
}
/**
* Save table DCI value
*/
bool RRDToolStorageDriver::saveDCTableValue(DCTable *dcObject, time_t timestamp, Table *value)
{
_tprintf(_T("SAVE TABLE: %s %d\n\n"), dcObject->getName(), value->getNumRows());
return true;
}
/**
* Driver entry point
*/
DECLARE_PDSDRV_ENTRY_POINT(s_driverName, RRDToolStorageDriver);
void RRDToolStorageDriver::error(const char *msg)
{
perror(msg);
exit(0);
}