collectagent.cpp 5.23 KB
Newer Older
1
2
3
4
5
//============================================================================
// Name        : CollectAgent.cpp
// Author      : Axel Auweter
// Version     :
// Copyright   : Leibniz Supercomputing Centre
6
// Description : Main code of the CollectAgent
7
8
//============================================================================

9
#include <cstdlib>
10
#include <signal.h>
11
12
#include <unistd.h>
#include <string>
13

14
#include <boost/date_time/posix_time/posix_time.hpp>
15

16
17
#include <dcdb/sensordatastore.h>

18
#include "simplemqttserver.h"
19
#include "messaging.h"
20

21
22
23
#define __STDC_FORMAT_MACROS
#include <inttypes.h>

24
using namespace std;
25
26

int keepRunning;
27
28
uint64_t msgCtr;
uint64_t pmsgCtr;
29
SensorDataStore *mySensorDataStore;
30
std::string host;
31

32

33
34
void sigHandler(int sig)
{
35
36
37
  keepRunning = 0;
}

38
39
void mqttCallback(SimpleMQTTMessage *msg)
{
40

41
42
43
  /*
   * Increment the msgCtr/vmsgCtr for statistics.
   */
44
  msgCtr++;
45
46
47
48
49
50
  if (msg->isPublish())
    pmsgCtr++;

  /*
   * Decode the message and put into the database.
   */
51
  if (msg->isPublish()) {
52
53
54
55
56
57
58
	  
	  uint64_t val;
	  uint64_t ts;
	  
	  //In the 64 bit message case, the collect agent provides a timestamp
	  if(msg->getPayloadLength()==sizeof(uint64_t)) {
		  
59
		  //printf("Providing timestamp...\n");
60
		  val = *((uint64_t*)msg->getPayload());
61
		  ts = Messaging::calculateTimestamp();
62
		  //printf("Val = %" PRIu64 ", timestamp = %" PRIu64 "\n", val, ts);
63
64
65
	   }
	   
	   //...otherwise it just retrieves it from the MQTT message payload. 
66
	   else if((msg->getPayloadLength()%sizeof(mqttPayload)==0) && (msg->getPayloadLength()>0)){
67
		   
68
		   //printf("Retrieving timestamp...\n");
69
		   mqttPayload *payload = (mqttPayload*)msg->getPayload();
70
		   
71
72
		   val = payload->value; // payload[n].value
		   ts = payload->timestamp;
73
		   //printf("Val = %" PRIu64 ", timestamp = %" PRIu64 "\n", val, ts);
74
	   }
75
76
77
78
79
80
81
82

      /*
       * Check if we can decode the message topic
       * into a valid SensorId. If successful, store
       * the record in the database.
       */
      SensorId sid;
      if (mySensorDataStore->topicToSid(&sid,msg->getTopic())) {
83
#if 0
84
          cout << "Topic decode successful:"
85
               << "\nRaw: " << hex << setw(16) << setfill('0') << sid.raw[0] << " " << hex << setw(16) << setfill('0') << sid.raw[1]
86
87
88
89
90
91
92
93
94
95
               << "\ndatacenter_id: " << hex << ((sid.dl & 0xFF00000000000000) >> 56)
               << "\ncluster_id:    " << hex << ((sid.dl & 0x00FF000000000000) >> 48)
               << "\nrack_id:       " << hex << ((sid.dl & 0x0000FFFF00000000) >> 32)
               << "\nchassis_id:    " << hex << ((sid.dl & 0x00000000FF000000) >> 24)
               << "\nbic_id:        " << hex << ((sid.dl & 0x0000000000FF0000) >> 16)
               << "\nbmc_id:        " << hex << ((sid.dl & 0x000000000000FF00) >> 8)
               << "\nknc_id:        " << hex << ((sid.dl & 0x00000000000000FF))
               << "\ndevice_id:     " << hex << sid.dsid.device_id
               << "\nreserved:      " << hex << sid.dsid.rsvd
               << "\nsensor_number: " << hex << sid.dsid.sensor_number
96
               << "\n";
97
#endif
98
          mySensorDataStore->insert(&sid, ts, val);
99
      }
100
101
102
103
104
#if 0
      else {
          cout << "Wrong topic format: " << msg->getTopic() << "\n";
      }
#endif
105
  }
106
  delete msg;
107
108
}

109
110
111
112
113
114
115
116
117
118
119
/*
 * Print usage information
 */
void usage() {
  printf("Usage: collectagent [-h <host>]\n");
  printf("Collectagent will accept remote connections by listening to the\n");
  printf("specified listen address <host> at port 1883 (default MQTT port).\n");
  printf("The default <host> is localhost.\n\n");
}

int main(int argc, char* const argv[]) {
120
121

  try{
122
      /*
123
       * Catch SIGINT signals to allow for proper server shutdowns.
124
       */
125
      signal(SIGINT, sigHandler);
126

127
128
      /* Parse command line */
      char ret;
129
130
      host="localhost";
      while ((ret=getopt(argc, argv, "h:?"))!=EOF) {
131
          switch(ret) {
132
133
134
135
136
137
138
              case 'h':
                  host = optarg;
                  break;
              case '?':
              default:
                  usage();
                  exit(EXIT_FAILURE);
139
140
141
          }
      }

142
143
144
      /*
       * Allocate and initialize sensor data store.
       */
145
      mySensorDataStore = new SensorDataStore("127.0.0.1", 9042);
146

147
148
149
      /*
       * Start the MQTT Message Server.
       */
150
151
      SimpleMQTTServer ms(host,"1883");
      
152
153
154
155
156
      ms.setMessageCallback(mqttCallback);
      ms.start();

      cout << "Server running...\n";

157
158
159
160
161
162
163
      /*
       * Run (hopefully) forever...
       */
      keepRunning = 1;
      timeval start, end;
      double elapsed;

164
165
      while(keepRunning) {
          gettimeofday(&start, NULL);
166
          sleep(60);
167
168
169
170
          /* not really thread safe but will do the job */
          gettimeofday(&end, NULL);
          elapsed = (end.tv_sec - start.tv_sec) * 1000.0;
          elapsed += (end.tv_usec - start.tv_usec) / 1000.0;
171
172
          float publish = msgCtr?(pmsgCtr*100.0)/msgCtr:0;
          cout << "Message rate: " << (msgCtr/elapsed)*1000.0 << " messages/second (" << publish << "% PUBLISH)\n";
173
          msgCtr = 0;
174
          pmsgCtr = 0;
175
176
      }

177
      cout << "Stopping...\n";
178
179

      ms.stop();
180
      delete mySensorDataStore;
181
  }
182
183
  catch (const exception& e) {
      cout << "Exception: " << e.what() << "\n";
184
      exit(EXIT_FAILURE);
185
186
  }

187
  return EXIT_SUCCESS;
188
}
189
190