22 #include <sys/socket.h>
24 #include <netinet/in.h>
31 # include <sys/statvfs.h>
32 # include <sys/sysinfo.h>
34 # include <sys/param.h>
35 # include <sys/mount.h>
37 # include <sys/statfs.h>
38 # else // if !CONFIG_CYGWIN
39 # include <sys/sysctl.h>
40 # endif // !CONFIG_CYGWIN
43 #include "mythtv/mythconfig.h"
46 #define MSG_NOSIGNAL 0 // Apple also has SO_NOSIGPIPE?
52 #define ZM_PROTOCOL_VERSION "7"
55 #define MAX_IMAGE_SIZE (2048*1536*3)
57 #define ADD_STR(list,s) list += s; list += "[]:[]";
60 #define ERROR_TOKEN_COUNT "Invalid token count"
61 #define ERROR_MYSQL_QUERY "Mysql Query Error"
62 #define ERROR_MYSQL_ROW "Mysql Get Row Error"
63 #define ERROR_FILE_OPEN "Cannot open event file"
64 #define ERROR_INVALID_MONITOR "Invalid Monitor"
65 #define ERROR_INVALID_POINTERS "Cannot get shared memory pointers"
66 #define ERROR_INVALID_MONITOR_FUNCTION "Invalid Monitor Function"
67 #define ERROR_INVALID_MONITOR_ENABLE_VALUE "Invalid Monitor Enable Value"
83 cout <<
"loading zm config from " << configfile << endl;
88 if ( (cfg = fopen(configfile.c_str(),
"r")) == NULL )
90 fprintf(stderr,
"Can't open %s\n", configfile.c_str());
94 while ( fgets( line,
sizeof(line), cfg ) != NULL )
96 char *line_ptr = line;
98 size_t chomp_len = strcspn( line_ptr,
"\r\n" );
99 line_ptr[chomp_len] =
'\0';
102 size_t white_len = strspn( line_ptr,
" \t" );
103 line_ptr += white_len;
106 if ( *line_ptr ==
'\0' || *line_ptr ==
'#' )
110 char *temp_ptr = line_ptr+strlen(line_ptr)-1;
111 while ( *temp_ptr ==
' ' || *temp_ptr ==
'\t' )
118 temp_ptr = strchr( line_ptr,
'=' );
121 fprintf(stderr,
"Invalid data in %s: '%s'\n", configfile.c_str(), line );
126 char *name_ptr = line_ptr;
127 char *val_ptr = temp_ptr+1;
135 while ( *temp_ptr ==
' ' || *temp_ptr ==
'\t' );
138 white_len = strspn( val_ptr,
" \t" );
139 val_ptr += white_len;
141 strncpy( val, val_ptr, strlen(val_ptr)+1 );
142 if ( strcasecmp( name_ptr,
"ZM_DB_HOST" ) == 0 )
g_server = val;
143 else if ( strcasecmp( name_ptr,
"ZM_DB_NAME" ) == 0 )
g_database = val;
144 else if ( strcasecmp( name_ptr,
"ZM_DB_USER" ) == 0 )
g_user = val;
145 else if ( strcasecmp( name_ptr,
"ZM_DB_PASS" ) == 0 )
g_password = val;
146 else if ( strcasecmp( name_ptr,
"ZM_PATH_WEB" ) == 0 )
g_webPath = val;
147 else if ( strcasecmp( name_ptr,
"ZM_PATH_BIN" ) == 0 )
g_binPath = val;
148 else if ( strcasecmp( name_ptr,
"ZM_WEB_USER" ) == 0 )
g_webUser = val;
149 else if ( strcasecmp( name_ptr,
"ZM_VERSION" ) == 0 )
g_zmversion = val;
158 cout <<
"Error: Can't initialise structure: " << mysql_error(&
g_dbConn) << endl;
165 cout <<
"Error: Can't connect to server: " << mysql_error(&
g_dbConn) << endl;
171 cout <<
"Error: Can't select database: " << mysql_error(&
g_dbConn) << endl;
182 cout <<
"Kicking database connection" << endl;
186 if (mysql_query(&
g_dbConn,
"SELECT NULL;") == 0)
188 MYSQL_RES *res = mysql_store_result(&
g_dbConn);
190 mysql_free_result(res);
194 cout <<
"Lost connection to DB - trying to reconnect" << endl;
204 cout <<
"Using server protocol version '" << ZM_PROTOCOL_VERSION <<
"'\n";
217 sscanf(setting.c_str(),
"%20llx", &
tmp);
223 snprintf(buf,
sizeof(buf),
"0x%x", (
unsigned int)
m_shmKey);
224 cout <<
"Shared memory key is: " << buf << endl;
231 cout <<
"Memory path directory is: " <<
m_mmapPath << endl;
236 int eventDigits = atoi(setting.c_str());
237 snprintf(buf,
sizeof(buf),
"%%0%dd-capture.jpg", eventDigits);
243 snprintf(buf,
sizeof(buf),
"%%0%dd-analyse.jpg", eventDigits);
253 cout <<
"using deep storage directory structure" << endl;
255 cout <<
"using flat directory structure" << endl;
264 cout <<
"ZMServer destroyed\n";
271 string::size_type startPos = 0;
272 string::size_type endPos = 0;
274 while((endPos = command.find(
"[]:[]", startPos)) != string::npos)
276 token = command.substr(startPos, endPos - startPos);
277 tokens.push_back(token);
278 startPos = endPos + 5;
282 if (endPos != command.length())
284 token = command.substr(startPos);
285 tokens.push_back(token);
296 int dataLen = atoi(len);
301 vector<string> tokens;
308 cout <<
"Processing: '" << tokens[0] <<
"'" << endl;
310 if (tokens[0] ==
"HELLO")
312 else if (tokens[0] ==
"GET_SERVER_STATUS")
314 else if (tokens[0] ==
"GET_MONITOR_STATUS")
316 else if (tokens[0] ==
"GET_EVENT_LIST")
318 else if (tokens[0] ==
"GET_EVENT_DATES")
320 else if (tokens[0] ==
"GET_EVENT_FRAME")
322 else if (tokens[0] ==
"GET_ANALYSE_FRAME")
324 else if (tokens[0] ==
"GET_LIVE_FRAME")
326 else if (tokens[0] ==
"GET_FRAME_LIST")
328 else if (tokens[0] ==
"GET_CAMERA_LIST")
330 else if (tokens[0] ==
"GET_MONITOR_LIST")
332 else if (tokens[0] ==
"DELETE_EVENT")
334 else if (tokens[0] ==
"DELETE_EVENT_LIST")
336 else if (tokens[0] ==
"RUN_ZMAUDIT")
338 else if (tokens[0] ==
"SET_MONITOR_FUNCTION")
341 send(
"UNKNOWN_COMMAND");
347 uint32_t
len = s.size();
349 sprintf(buf,
"%8d", len);
355 status =
::send(
m_sock, s.c_str(), s.size(), MSG_NOSIGNAL);
365 uint32_t
len = s.size();
367 sprintf(buf,
"%8d", len);
373 status =
::send(
m_sock, s.c_str(), s.size(), MSG_NOSIGNAL);
388 ADD_STR(outStr,
string(
"ERROR - ") + error);
397 ADD_STR(outStr,
"OK");
398 ADD_STR(outStr, ZM_PROTOCOL_VERSION);
405 memset(&statbuf, 0,
sizeof(statbuf));
406 long long freespace = -1;
414 if ((
statfs(filename.c_str(), &statbuf) == 0) &&
424 freespace = freespace >> 10;
426 used = total - freespace;
435 ADD_STR(outStr,
"OK")
439 ADD_STR(outStr, status)
443 if (getloadavg(loads, 3) == -1)
445 ADD_STR(outStr,
"Unknown")
450 sprintf(buf,
"%0.2lf", loads[0]);
456 long long total, used;
457 string eventsDir =
g_webPath +
"/events/";
459 sprintf(buf,
"%d%%", (
int) ((100.0 / ((
float) total / used))));
469 if (tokens.size() != 4)
475 string monitor = tokens[1];
476 bool oldestFirst = (tokens[2] ==
"1");
477 string date = tokens[3];
480 cout <<
"Loading events for monitor: " << monitor <<
", date: " << date << endl;
482 ADD_STR(outStr,
"OK")
487 string sql(
"SELECT E.Id, E.Name, M.Id AS MonitorID, M.Name AS MonitorName, E.StartTime, "
488 "E.Length, M.Width, M.Height, M.DefaultRate, M.DefaultScale "
489 "from Events as E inner join Monitors as M on E.MonitorId = M.Id ");
491 if (monitor !=
"<ANY>")
493 sql +=
"WHERE M.Name = '" + monitor +
"' ";
496 sql +=
"AND DATE(E.StartTime) = DATE('" + date +
"') ";
501 sql +=
"WHERE DATE(E.StartTime) = DATE('" + date +
"') ";
505 sql +=
"ORDER BY E.StartTime ASC";
507 sql +=
"ORDER BY E.StartTime DESC";
509 if (mysql_query(&
g_dbConn, sql.c_str()))
511 fprintf(stderr,
"%s\n", mysql_error(&
g_dbConn));
516 res = mysql_store_result(&
g_dbConn);
517 int eventCount = mysql_num_rows(res);
520 cout <<
"Got " << eventCount <<
" events" << endl;
523 sprintf(str,
"%d", eventCount);
526 for (
int x = 0;
x < eventCount;
x++)
528 row = mysql_fetch_row(res);
531 ADD_STR(outStr, row[0])
532 ADD_STR(outStr, row[1])
533 ADD_STR(outStr, row[2])
534 ADD_STR(outStr, row[3])
536 ADD_STR(outStr, row[4])
537 ADD_STR(outStr, row[5])
541 cout <<
"Failed to get mysql row" << endl;
547 mysql_free_result(res);
556 if (tokens.size() != 3)
562 string monitor = tokens[1];
563 bool oldestFirst = (tokens[2] ==
"1");
566 cout <<
"Loading event dates for monitor: " << monitor << endl;
568 ADD_STR(outStr,
"OK")
573 string sql(
"SELECT DISTINCT DATE(E.StartTime) "
574 "from Events as E inner join Monitors as M on E.MonitorId = M.Id ");
576 if (monitor !=
"<ANY>")
577 sql +=
"WHERE M.Name = '" + monitor +
"' ";
580 sql +=
"ORDER BY E.StartTime ASC";
582 sql +=
"ORDER BY E.StartTime DESC";
584 if (mysql_query(&
g_dbConn, sql.c_str()))
586 fprintf(stderr,
"%s\n", mysql_error(&
g_dbConn));
591 res = mysql_store_result(&
g_dbConn);
592 int dateCount = mysql_num_rows(res);
595 cout <<
"Got " << dateCount <<
" dates" << endl;
598 sprintf(str,
"%d", dateCount);
601 for (
int x = 0;
x < dateCount;
x++)
603 row = mysql_fetch_row(res);
606 ADD_STR(outStr, row[0])
610 cout <<
"Failed to get mysql row" << endl;
616 mysql_free_result(res);
624 ADD_STR(outStr,
"OK")
630 string sql(
"SELECT Id, Name, Type, Device, Host, Channel, Function, Enabled "
632 if (mysql_query(&
g_dbConn, sql.c_str()))
634 fprintf(stderr,
"%s\n", mysql_error(&
g_dbConn));
639 res = mysql_store_result(&
g_dbConn);
642 int monitorCount = mysql_num_rows(res);
645 cout <<
"Got " << monitorCount <<
" monitors" << endl;
648 sprintf(str,
"%d", monitorCount);
651 for (
int x = 0;
x < monitorCount;
x++)
653 row = mysql_fetch_row(res);
657 string type = row[2];
658 string device = row[3];
659 string host = row[4];
660 string channel = row[5];
661 string function = row[6];
662 string enabled = row[7];
663 string name = row[1];
665 string zmcStatus =
"";
666 string zmaStatus =
"";
668 zmcStatus, zmaStatus, enabled);
672 string sql2(
"SELECT count(if(Archived=0,1,NULL)) AS EventCount "
674 "WHERE MonitorId = " +
id);
676 if (mysql_query(&
g_dbConn, sql2.c_str()))
678 fprintf(stderr,
"%s\n", mysql_error(&
g_dbConn));
683 res2 = mysql_store_result(&
g_dbConn);
684 if (mysql_num_rows(res2) > 0)
686 row2 = mysql_fetch_row(res2);
691 cout <<
"Failed to get mysql row" << endl;
698 ADD_STR(outStr, name)
699 ADD_STR(outStr, zmcStatus)
700 ADD_STR(outStr, zmaStatus)
701 ADD_STR(outStr, events)
702 ADD_STR(outStr,
function)
703 ADD_STR(outStr, enabled)
705 mysql_free_result(res2);
709 cout <<
"Failed to get mysql row" << endl;
715 mysql_free_result(res);
723 FILE *fd = popen(command.c_str(),
"r");
726 while (fgets(buffer,
sizeof(buffer), fd) != NULL)
735 string function,
string &zmcStatus,
string &zmaStatus,
741 string command(
g_binPath +
"/zmdc.pl status");
747 zmaStatus = device +
"(" + channel +
") [-]";
748 else if (status.find(
"'zma -m " +
id +
"' running") != string::npos)
749 zmaStatus = device +
"(" + channel +
") [R]";
751 zmaStatus = device +
"(" + channel +
") [S]";
756 zmaStatus = host +
" [-]";
757 else if (status.find(
"'zma -m " +
id +
"' running") != string::npos)
758 zmaStatus = host +
" [R]";
760 zmaStatus = host +
" [S]";
766 zmcStatus =
function +
" [-]";
767 else if (status.find(
"'zmc -d "+ device +
"' running") != string::npos)
768 zmcStatus =
function +
" [R]";
770 zmcStatus =
function +
" [S]";
775 zmcStatus =
function +
" [-]";
776 else if (status.find(
"'zmc -m " +
id +
"' running") != string::npos)
777 zmcStatus =
function +
" [R]";
779 zmcStatus =
function +
" [S]";
785 static unsigned char buffer[MAX_IMAGE_SIZE];
787 if (tokens.size() != 5)
793 string monitorID(tokens[1]);
794 string eventID(tokens[2]);
795 int frameNo = atoi(tokens[3].c_str());
796 string eventTime(tokens[4]);
799 cout <<
"Getting frame " << frameNo <<
" for event " << eventID
800 <<
" on monitor " << monitorID <<
" event time is " << eventTime << endl;
804 ADD_STR(outStr,
"OK")
812 filepath =
g_webPath +
"/events/" + monitorID +
"/" + eventTime +
"/";
818 filepath =
g_webPath +
"/events/" + monitorID +
"/" + eventID +
"/";
825 if ((fd = fopen(filepath.c_str(),
"r" )))
827 fileSize = fread(buffer, 1,
sizeof(buffer), fd);
832 cout <<
"Can't open " << filepath <<
": " << strerror(
errno) << endl;
833 sendError(ERROR_FILE_OPEN +
string(
" - ") + filepath +
" : " + strerror(
errno));
838 cout <<
"Frame size: " << fileSize << endl;
841 sprintf(str,
"%d", fileSize);
845 send(outStr, buffer, fileSize);
850 static unsigned char buffer[MAX_IMAGE_SIZE];
853 if (tokens.size() != 5)
859 string monitorID(tokens[1]);
860 string eventID(tokens[2]);
861 int frameNo = atoi(tokens[3].c_str());
862 string eventTime(tokens[4]);
865 cout <<
"Getting anaylse frame " << frameNo <<
" for event " << eventID
866 <<
" on monitor " << monitorID <<
" event time is " << eventTime << endl;
870 MYSQL_ROW row = NULL;
873 sql +=
"SELECT FrameId FROM Frames ";
874 sql +=
"WHERE EventID = " + eventID +
" ";
875 sql +=
"AND Type = 'Alarm' ";
876 sql +=
"ORDER BY FrameID";
878 if (mysql_query(&
g_dbConn, sql.c_str()))
880 fprintf(stderr,
"%s\n", mysql_error(&
g_dbConn));
885 res = mysql_store_result(&
g_dbConn);
886 int frameCount = mysql_num_rows(res);
890 if (frameNo == 0 || frameNo < 0 || frameNo > frameCount)
891 frameNo = (frameCount / 2) + 1;
894 for (
int x = 0;
x < frameNo;
x++)
896 row = mysql_fetch_row(res);
901 frameID = atoi(row[0]);
905 cout <<
"handleGetAnalyseFrame: Failed to get mysql row for frameNo " << frameNo << endl;
912 ADD_STR(outStr,
"OK")
918 filepath =
g_webPath +
"/events/" + monitorID +
"/" + eventTime +
"/";
924 filepath =
g_webPath +
"/events/" + monitorID +
"/" + eventID +
"/";
931 if ((fd = fopen(filepath.c_str(),
"r" )))
933 fileSize = fread(buffer, 1,
sizeof(buffer), fd);
938 cout <<
"Can't open " << filepath <<
": " << strerror(
errno) << endl;
939 sendError(ERROR_FILE_OPEN +
string(
" - ") + filepath +
" : " + strerror(
errno));
944 cout <<
"Frame size: " << fileSize << endl;
947 sprintf(str,
"%d", fileSize);
951 send(outStr, buffer, fileSize);
956 static unsigned char buffer[MAX_IMAGE_SIZE];
965 if (tokens.size() != 2)
971 int monitorID = atoi(tokens[1].c_str());
974 cout <<
"Getting live frame from monitor: " << monitorID << endl;
978 ADD_STR(outStr,
"OK")
981 sprintf(str,
"%d", monitorID);
1002 int dataSize =
getFrame(buffer,
sizeof(buffer), monitor);
1005 cout <<
"Frame size: " << dataSize << endl;
1011 ADD_STR(outStr,
"WARNING - No new frame available");
1017 ADD_STR(outStr, monitor->
status)
1020 sprintf(str,
"%d", dataSize);
1021 ADD_STR(outStr, str)
1024 send(outStr, buffer, dataSize);
1032 if (tokens.size() != 2)
1038 eventID = tokens[1];
1041 cout <<
"Loading frames for event: " << eventID << endl;
1043 ADD_STR(outStr,
"OK")
1049 sql +=
"SELECT Type, Delta FROM Frames ";
1050 sql +=
"WHERE EventID = " + eventID +
" ";
1051 sql +=
"ORDER BY FrameID";
1053 if (mysql_query(&
g_dbConn, sql.c_str()))
1055 fprintf(stderr,
"%s\n", mysql_error(&
g_dbConn));
1060 res = mysql_store_result(&
g_dbConn);
1061 int frameCount = mysql_num_rows(res);
1064 cout <<
"Got " << frameCount <<
" frames" << endl;
1067 sprintf(str,
"%d\n", frameCount);
1068 ADD_STR(outStr, str)
1070 for (
int x = 0;
x < frameCount;
x++)
1072 row = mysql_fetch_row(res);
1075 ADD_STR(outStr, row[0])
1076 ADD_STR(outStr, row[1])
1080 cout <<
"handleGetFrameList: Failed to get mysql row " <<
x << endl;
1086 mysql_free_result(res);
1095 ADD_STR(outStr,
"OK")
1101 sql +=
"SELECT DISTINCT M.Name FROM Events AS E ";
1102 sql +=
"INNER JOIN Monitors AS M ON E.MonitorId = M.Id;";
1104 if (mysql_query(&
g_dbConn, sql.c_str()))
1106 fprintf(stderr,
"%s\n", mysql_error(&
g_dbConn));
1111 res = mysql_store_result(&
g_dbConn);
1112 int monitorCount = mysql_num_rows(res);
1114 sprintf(str,
"%d", monitorCount);
1115 ADD_STR(outStr, str)
1117 for (
int x = 0;
x < monitorCount;
x++)
1119 row = mysql_fetch_row(res);
1122 ADD_STR(outStr, row[0])
1126 cout <<
"handleGetCameraList: Failed to get mysql row " <<
x << endl;
1132 mysql_free_result(res);
1141 ADD_STR(outStr,
"OK")
1147 sql +=
"SELECT Id, Name, Width, Height, Palette FROM Monitors ORDER BY Id";
1149 if (mysql_query(&
g_dbConn, sql.c_str()))
1151 fprintf(stderr,
"%s\n", mysql_error(&
g_dbConn));
1156 res = mysql_store_result(&
g_dbConn);
1157 int monitorCount = mysql_num_rows(res);
1160 cout <<
"Got " << monitorCount <<
" monitors" << endl;
1163 sprintf(str,
"%d", monitorCount);
1164 ADD_STR(outStr, str)
1166 for (
int x = 0;
x < monitorCount;
x++)
1168 row = mysql_fetch_row(res);
1171 ADD_STR(outStr, row[0])
1172 ADD_STR(outStr, row[1])
1173 ADD_STR(outStr, row[2])
1174 ADD_STR(outStr, row[3])
1175 ADD_STR(outStr, row[4])
1179 cout <<
"id: " << row[0] << endl;
1180 cout <<
"name: " << row[1] << endl;
1181 cout <<
"width: " << row[2] << endl;
1182 cout <<
"height: " << row[3] << endl;
1183 cout <<
"palette: " << row[4] << endl;
1184 cout <<
"-------------------" << endl;
1189 cout <<
"Failed to get mysql row" << endl;
1195 mysql_free_result(res);
1205 if (tokens.size() != 2)
1211 eventID = tokens[1];
1214 cout <<
"Deleting event: " << eventID << endl;
1216 ADD_STR(outStr,
"OK")
1219 sql +=
"DELETE FROM Events WHERE Id = " + eventID;
1221 if (mysql_query(&
g_dbConn, sql.c_str()))
1223 fprintf(stderr,
"%s\n", mysql_error(&
g_dbConn));
1229 string command(
g_binPath +
"/zmaudit.pl &");
1231 if (system(command.c_str()) < 0 &&
errno)
1232 cerr <<
"Failed to run '" << command <<
"'" << endl;
1239 string eventList(
"");
1242 vector<string>::iterator it = tokens.begin();
1243 if (it != tokens.end())
1245 while (it != tokens.end())
1247 if (eventList ==
"")
1250 eventList +=
"," + (*it);
1256 cout <<
"Deleting events: " << eventList << endl;
1259 sql +=
"DELETE FROM Events WHERE Id IN (" + eventList +
")";
1261 if (mysql_query(&
g_dbConn, sql.c_str()))
1263 fprintf(stderr,
"%s\n", mysql_error(&
g_dbConn));
1268 ADD_STR(outStr,
"OK")
1277 string command(
g_binPath +
"/zmaudit.pl &");
1280 cout <<
"Running command: " << command << endl;
1283 if (system(command.c_str()) < 0 &&
errno)
1284 cerr <<
"Failed to run '" << command <<
"'" << endl;
1286 ADD_STR(outStr,
"OK")
1294 string sql(
"SELECT Id, Name, Width, Height, ImageBufferCount, MaxFPS, Palette, ");
1295 sql +=
" Type, Function, Enabled, Device, Host, Controllable, TrackMotion ";
1296 sql +=
"FROM Monitors";
1301 if (mysql_query(&
g_dbConn, sql.c_str()))
1303 fprintf(stderr,
"%s\n", mysql_error(&
g_dbConn));
1307 res = mysql_store_result(&
g_dbConn);
1308 int monitorCount = mysql_num_rows(res);
1311 cout <<
"Got " << monitorCount <<
" monitors" << endl;
1313 for (
int x = 0;
x < monitorCount;
x++)
1315 row = mysql_fetch_row(res);
1319 m->
mon_id = atoi(row[0]);
1321 m->
width = atoi(row[2]);
1322 m->
height = atoi(row[3]);
1338 cout <<
"Failed to get mysql row" << endl;
1343 mysql_free_result(res);
1348 void *shm_ptr = NULL;
1358 int shared_data_size;
1372 #if _POSIX_MAPPED_FILES > 0L
1377 stringstream mmap_filename;
1380 int mapFile =
open(mmap_filename.str().c_str(), O_RDONLY, 0x0);
1384 cout <<
"Opened mmap file: " << mmap_filename << endl;
1386 shm_ptr = mmap(NULL, shared_data_size, PROT_READ,
1387 MAP_SHARED, mapFile, 0
x0);
1388 if (shm_ptr == MAP_FAILED)
1390 cout <<
"Failed to map shared memory from file [" <<
1391 mmap_filename <<
"] " <<
1395 monitor->
status =
"Error";
1405 cout <<
"Failed to open mmap file [" <<
1406 mmap_filename <<
"] " <<
1409 " : " << strerror(
errno) <<
1411 cout <<
"Falling back to the legacy shared memory method" << endl;
1416 if (shm_ptr == NULL)
1422 shared_data_size, SHM_R)) == -1)
1424 cout <<
"Failed to shmget for monitor: " << monitor->
mon_id << endl;
1425 monitor->
status =
"Error";
1428 case EACCES: cout <<
"EACCES - no rights to access segment\n";
break;
1429 case EEXIST: cout <<
"EEXIST - segment already exists\n";
break;
1430 case EINVAL: cout <<
"EINVAL - size < SHMMIN or size > SHMMAX\n";
break;
1431 case ENFILE: cout <<
"ENFILE - limit on open files has been reached\n";
break;
1432 case ENOENT: cout <<
"ENOENT - no segment exists for the given key\n";
break;
1433 case ENOMEM: cout <<
"ENOMEM - couldn't reserve memory for segment\n";
break;
1434 case ENOSPC: cout <<
"ENOSPC - shmmni or shmall limit reached\n";
break;
1440 shm_ptr = shmat(shmid, 0, SHM_RDONLY);
1443 if (shm_ptr == NULL)
1445 cout <<
"Failed to shmat for monitor: " << monitor->
mon_id << endl;
1446 monitor->
status =
"Error";
1483 monitor->
status =
"Idle";
1486 monitor->
status =
"Pre Alarm";
1489 monitor->
status =
"Alarm";
1492 monitor->
status =
"Alert";
1495 monitor->
status =
"Tape";
1498 monitor->
status =
"Unknown";
1516 string sql(
"SELECT Name, Value FROM Config ");
1517 sql +=
"WHERE Name = '" + setting +
"'";
1522 if (mysql_query(&
g_dbConn, sql.c_str()))
1524 fprintf(stderr,
"%s\n", mysql_error(&
g_dbConn));
1528 res = mysql_store_result(&
g_dbConn);
1529 row = mysql_fetch_row(res);
1536 cout <<
"Failed to get mysql row" << endl;
1541 cout <<
"getZMSetting: " << setting <<
" Result: " << result << endl;
1543 mysql_free_result(res);
1552 if (tokens.size() != 4)
1558 string monitorID(tokens[1]);
1559 string function(tokens[2]);
1560 string enabled(tokens[3]);
1573 sendError(ERROR_INVALID_MONITOR_FUNCTION);
1577 if (enabled !=
"0" && enabled !=
"1")
1579 sendError(ERROR_INVALID_MONITOR_ENABLE_VALUE);
1584 cout <<
"User input validated OK" << endl;
1589 string oldFunction = monitor->
function;
1590 string newFunction =
function;
1591 int oldEnabled = monitor->
enabled;
1592 int newEnabled = atoi(enabled.c_str());
1594 monitor->
enabled = newEnabled;
1597 cout <<
"SetMonitorFunction MonitorId: " << monitorID << endl <<
1598 " oldEnabled: " << oldEnabled << endl <<
1599 " newEnabled: " << newEnabled << endl <<
1600 " oldFunction: " << oldFunction << endl <<
1601 " newFunction: " << newFunction << endl;
1603 if ( newFunction != oldFunction || newEnabled != oldEnabled)
1605 string sql(
"UPDATE Monitors ");
1606 sql +=
"SET Function = '" +
function +
"', ";
1607 sql +=
"Enabled = '" + enabled +
"' ";
1608 sql +=
"WHERE Id = '" + monitorID +
"'";
1610 if (mysql_query(&
g_dbConn, sql.c_str()))
1612 fprintf(stderr,
"%s\n", mysql_error(&
g_dbConn));
1618 cout <<
"Monitor function SQL update OK" << endl;
1626 cout <<
"Monitor function Refreshing daemons" << endl;
1630 (newEnabled != oldEnabled);
1640 cout <<
"zm daemons are not running" << endl;
1643 cout <<
"Not updating monitor function as identical to existing configuration" << endl;
1645 ADD_STR(outStr,
"OK")
1651 string zmcArgs =
"";
1653 sql +=
"SELECT count(if(Function!='None',1,NULL)) as ActiveCount ";
1654 sql +=
"FROM Monitors ";
1656 if (monitor->
type ==
"Local" )
1658 sql +=
"WHERE Device = '" + monitor->
device +
"'";
1659 zmcArgs =
"-d " + monitor->
device;
1663 sql +=
"WHERE Id = '" + monitor->
getIdStr() +
"'";
1664 zmcArgs =
"-m " + monitor->
mon_id;
1667 if (mysql_query(&
g_dbConn, sql.c_str()))
1669 fprintf(stderr,
"%s\n", mysql_error(&
g_dbConn));
1678 res = mysql_store_result(&
g_dbConn);
1679 row = mysql_fetch_row(res);
1682 activeCount = atoi(row[0]);
1702 int zmOptControl = atoi(
getZMSetting(
"ZM_OPT_CONTROL").c_str());
1703 int zmOptFrameServer = atoi(
getZMSetting(
"ZM_OPT_FRAME_SERVER").c_str());
1717 if (zmOptFrameServer)
1721 if (zmOptFrameServer)
1740 if (zmOptFrameServer)