Commit d056354d authored by Axel Auweter's avatar Axel Auweter
Browse files

Add CGI interface for DCDBQuery.

parent 6bd94015
include ../../config.mk
CXXFLAGS = -O2 -ggdb --std=c++11 -Wall -Wno-unused-local-typedefs -Wno-unknown-warning-option -fmessage-length=0 -I$(DCDBDEPLOYPATH)/include/ -I$(DCDBBASEPATH)/include/ -DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
CXXFLAGS = -O0 -ggdb --std=c++11 -Wall -Wno-unused-local-typedefs -Wno-unknown-warning-option -fmessage-length=0 -I$(DCDBDEPLOYPATH)/include/ -I$(DCDBBASEPATH)/include/ -DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
OBJS = dcdbquery.o query.o
LIBS = -L$(DCDBDEPLOYPATH)/lib/ -ldcdb -lcassandra -luv -lboost_random -lboost_system -lboost_date_time -lboost_regex -lssl -lcrypto
TARGET = dcdbquery
......
......@@ -19,8 +19,9 @@
void usage(void)
{
/* 0---------1---------2---------3---------4---------5---------6---------7--------- */
std::cout << "Usage:" << std::endl
if (isatty(fileno(stdin))) {
/* 0---------1---------2---------3---------4---------5---------6---------7--------- */
std::cout << "Usage:" << std::endl
<< "dcdbquery [-h <hostname>] [-r] [-l] <Sensor 1> [<Sensor 2> ...] <Start> <End>" << std::endl
<< "where" << std::endl
<< " <hostname> - the name of the DB server" << std::endl
......@@ -34,13 +35,47 @@ void usage(void)
<< "format (nanoseconds since epoch) insted of ISO human readable form." << std::endl
<< "When the -l option is specified, times for <Start>, <End>, and in the generated" << std::endl
<< "output will be interpreted / printed in the local time zone." << std::endl;
}
else {
std::cout << "Invalid request." << std::endl;
}
exit(EXIT_SUCCESS);
}
int main(int argc, char* argv[])
int main(int argc, char * const argv[])
{
/* Check if run from command line */
int argcReal;
char **argvReal;
if (isatty(fileno(stdin))) {
argcReal = argc;
argvReal = (char**)argv;
}
else {
/* Check if we are a CGI program */
if (!getenv("QUERY_STRING")) {
std::cout << "No terminal and no QUERY_STRING environment variable." << std::endl;
std::cout << "Exiting." << std::endl;
exit(EXIT_FAILURE);
}
/* Create argcReal and argvReal */
argcReal = 1;
argvReal = (char**)malloc(sizeof(char*));
argvReal[0] = strdup("dcdbquery"); /* dummy to make consistent with command line invocation */
char *token = strtok(getenv("QUERY_STRING"), "&");
while (token) {
argcReal++;
argvReal = (char**)realloc((void*)argvReal, argcReal*sizeof(char*));
/* FIXME: should check if argvReal is NULL */
argvReal[argcReal-1] = token;
token = strtok(NULL, "&");
}
}
/* Check command line arguments */
if (argc <= 3) {
if (argcReal <= 3) {
usage();
}
......@@ -55,7 +90,7 @@ int main(int argc, char* argv[])
host = "localhost";
}
while ((ret=getopt(argc, argv, "+h:rlf"))!=-1) {
while ((ret=getopt(argcReal, argvReal, "+h:rlf"))!=-1) {
switch(ret) {
case 'h':
host = optarg;
......@@ -79,8 +114,8 @@ int main(int argc, char* argv[])
DCDB::TimeStamp start, end;
try {
bool local = myQuery->getLocalTimeEnabled();
start = DCDB::TimeStamp(argv[argc-2], local);
end = DCDB::TimeStamp(argv[argc-1], local);
start = DCDB::TimeStamp(argvReal[argcReal-2], local);
end = DCDB::TimeStamp(argvReal[argcReal-1], local);
}
catch (std::exception& e) {
std::cout << "Wrong time format." << std::endl;
......@@ -95,8 +130,8 @@ int main(int argc, char* argv[])
/* Build a list of sensornames */
std::list<std::string> sensors;
for (int arg = optind; arg < argc-2; arg++) {
sensors.push_back(argv[arg]);
for (int arg = optind; arg < argcReal-2; arg++) {
sensors.push_back(argvReal[arg]);
}
myQuery->doQuery(host, sensors, start, end);
......
......@@ -123,6 +123,7 @@ void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DC
connection->setHostname(hostname);
if (!connection->connect()) {
std::cout << "Cannot connect to database." << std::endl;
exit(EXIT_FAILURE);
}
/* Initialize the SensorConfig interface */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment