collectagent.cpp 4.36 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

20
using namespace std;
21
22

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

28
29
void sigHandler(int sig)
{
30
31
32
  keepRunning = 0;
}

33
34
35
36
37
void mqttCallback(SimpleMQTTMessage *msg)
{
  /*
   * Increment the msgCtr/vmsgCtr for statistics.
   */
38
  msgCtr++;
39
40
41
42
43
44
  if (msg->isPublish())
    pmsgCtr++;

  /*
   * Decode the message and put into the database.
   */
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  if (msg->isPublish()) {
      /*
       * Calculate Time Stamp.
       */
      boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
      boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
      boost::posix_time::time_duration diff = now - epoch;

      uint64_t ts = diff.total_nanoseconds();

      /*
       * 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())) {
62
#if 0
63
64
65
66
67
68
69
70
71
72
73
74
75
          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";
76
77
#endif

78
          mySensorDataStore->insert(&sid, ts, *((uint64_t*)msg->getPayload()));
79
      }
80
  }
81
  delete msg;
82
83
}

84
85
86
87
88
89
90
91
92
93
94
/*
 * 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[]) {
95
96

  try{
97
      /*
98
       * Catch SIGINT signals to allow for proper server shutdowns.
99
       */
100
      signal(SIGINT, sigHandler);
101
102

      /*
103
       * Allocate and initialize sensor data store.
104
       */
105
      mySensorDataStore = new SensorDataStore();
106

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
      /* Parse command line */
      char ret;
	  host="localhost";
      while ((ret=getopt(argc, argv, "h:"))!=EOF) {
          switch(ret) {
          case 'h':
            host = optarg;
            break;
          case '?':
          default:
			usage();
			exit(EXIT_FAILURE);
          }
      }

122
123
124
      /*
       * Start the MQTT Message Server.
       */
125
126
      SimpleMQTTServer ms(host,"1883");
      
127
128
129
130
131
      ms.setMessageCallback(mqttCallback);
      ms.start();

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

132
133
134
135
136
137
138
      /*
       * Run (hopefully) forever...
       */
      keepRunning = 1;
      timeval start, end;
      double elapsed;

139
140
      while(keepRunning) {
          gettimeofday(&start, NULL);
141
          sleep(60);
142
143
144
145
          /* 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;
146
147
          float publish = msgCtr?(pmsgCtr*100.0)/msgCtr:0;
          cout << "Message rate: " << (msgCtr/elapsed)*1000.0 << " messages/second (" << publish << "% PUBLISH)\n";
148
          msgCtr = 0;
149
          pmsgCtr = 0;
150
151
      }

152
      cout << "Stopping...\n";
153
154

      ms.stop();
155
      delete mySensorDataStore;
156
  }
157
158
  catch (const exception& e) {
      cout << "Exception: " << e.what() << "\n";
159
      exit(EXIT_FAILURE);
160
161
  }

162
  return EXIT_SUCCESS;
163
}
164
165