collectagent.cpp 4.75 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
using namespace std;
22
23

int keepRunning;
24
25
uint64_t msgCtr;
uint64_t pmsgCtr;
26
SensorDataStore *mySensorDataStore;
27
std::string host;
28

29

30
31
void sigHandler(int sig)
{
32
33
34
  keepRunning = 0;
}

35
36
void mqttCallback(SimpleMQTTMessage *msg)
{
37

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

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

      /*
       * 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())) {
76
#if 0
77
78
79
80
81
82
83
84
85
86
87
88
89
          cout << "Topic decode successful:"
               << "\nRaw: " << sid.raw[0] << " " << sid.raw[1]
               << "\ndatacenter_id: " << hex << (uint32_t)sid.dl.datacenter_id
               << "\ncluster_id: " << hex << (uint32_t)sid.dl.cluster_id
               << "\nrack_id_msb: " << hex << (uint32_t)sid.dl.rack_id_msb
               << "\nrack_id_lsb: " << hex << (uint32_t)sid.dl.rack_id_lsb
               << "\nchassis_id: " << hex << (uint32_t)sid.dl.chassis_id
               << "\nbic_id: " << hex << (uint32_t)sid.dl.bic_id
               << "\nbnc_id: " << hex << (uint32_t)sid.dl.bnc_id
               << "\nknc_id: " << hex << (uint32_t)sid.dl.knc_id
               << "\ndevice_id: " << hex << sid.dsid.device_id
               << "\nsensor_id: " << hex << sid.dsid.sensor_id
               << "\n";
90
91
#endif

92
93
          //mySensorDataStore->insert(&sid, ts, *((uint64_t*)msg->getPayload()));
          mySensorDataStore->insert(&sid, ts, val);
94
      }
95
  }
96
  delete msg;
97
98
}

99
100
101
102
103
104
105
106
107
108
109
/*
 * 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[]) {
110
111

  try{
112
      /*
113
       * Catch SIGINT signals to allow for proper server shutdowns.
114
       */
115
      signal(SIGINT, sigHandler);
116

117
118
      /* Parse command line */
      char ret;
119
120
      host="localhost";
      while ((ret=getopt(argc, argv, "h:?"))!=EOF) {
121
          switch(ret) {
122
123
124
125
126
127
128
              case 'h':
                  host = optarg;
                  break;
              case '?':
              default:
                  usage();
                  exit(EXIT_FAILURE);
129
130
131
          }
      }

132
133
134
135
136
      /*
       * Allocate and initialize sensor data store.
       */
      mySensorDataStore = new SensorDataStore();

137
138
139
      /*
       * Start the MQTT Message Server.
       */
140
141
      SimpleMQTTServer ms(host,"1883");
      
142
143
144
145
146
      ms.setMessageCallback(mqttCallback);
      ms.start();

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

147
148
149
150
151
152
153
      /*
       * Run (hopefully) forever...
       */
      keepRunning = 1;
      timeval start, end;
      double elapsed;

154
155
      while(keepRunning) {
          gettimeofday(&start, NULL);
156
          sleep(60);
157
158
159
160
          /* 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;
161
162
          float publish = msgCtr?(pmsgCtr*100.0)/msgCtr:0;
          cout << "Message rate: " << (msgCtr/elapsed)*1000.0 << " messages/second (" << publish << "% PUBLISH)\n";
163
          msgCtr = 0;
164
          pmsgCtr = 0;
165
166
      }

167
      cout << "Stopping...\n";
168
169

      ms.stop();
170
      delete mySensorDataStore;
171
  }
172
173
  catch (const exception& e) {
      cout << "Exception: " << e.what() << "\n";
174
      exit(EXIT_FAILURE);
175
176
  }

177
  return EXIT_SUCCESS;
178
}
179
180