Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.25 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string>
  7. #include <sstream>
  8. #include <map>
  9. #include <mysql_driver.h>
  10. #include <mysql_connection.h>
  11. #include <cppconn/statement.h>
  12. #include <unistd.h>
  13.  
  14. using namespace std;
  15.  
  16. bool verbose = false;
  17. bool reallyquery = true;
  18. fstream tfile;
  19. map<string, string> conf;
  20. stringstream query;
  21.  
  22. sql::mysql::MySQL_Driver *driver;
  23. sql::Connection *con;
  24. sql::Statement *stmt;
  25.  
  26.  
  27. map< int, map<int, string> > readFile(string name, bool fixcolons = false)
  28. {
  29. ifstream whatfile(name.c_str());
  30.  
  31. if(whatfile.fail()) cout << "Failed.";
  32. string line;
  33. int li = 1;
  34. map< int, map<int, string> > themap;
  35. while(whatfile.good()) {
  36. getline(whatfile, line);
  37. //cout << line << endl;
  38. if(fixcolons) {
  39. for(int i = 0; line.find(":", i) != line.npos; )
  40. {
  41. line.insert(i+1, " ");
  42. i = line.find(":", i + 1);
  43. }
  44. }
  45. for(int i = 0; line.find(" ") != line.npos; )
  46. {
  47. i = line.find(" ");
  48. line.replace(i, 2, " ");
  49. }
  50. if(line.find(" ") == line.npos)
  51. {
  52. themap[li][0] = line;
  53. line = "";
  54. cout << "+++ " << themap[li][0]<<endl;
  55. }
  56. else
  57. {
  58. for(int i = 0; !line.empty(); i++)
  59. {
  60. if(line.find(" ") == line.npos) break;
  61. //cout << line << endl;
  62. int s = line.find(" ");
  63. if(i == 0) s = 0;
  64. int e = line.find(" ", s + 1);
  65. themap[li][i] = line.substr(s, e - s);
  66. //cout << "themap[" << li << "][" << i << "] = " << themap[li][i] << endl;
  67. if(!line.empty()) line.erase(s, e - s);
  68. }
  69. }
  70. li++;
  71. }
  72. whatfile.close();
  73.  
  74. //cout << themap[2][2] << endl;
  75. return themap;
  76. }
  77. map< int, map<int, string> > doCmd(string cmd, bool fixcolons = false)
  78. {
  79. cmd.append(" > .logtmp");
  80. system(cmd.c_str());
  81.  
  82. map< int, map<int, string> > out = readFile(".logtmp", fixcolons);
  83. system("rm .logtmp");
  84. return out;
  85. }
  86. int readConf()
  87. {
  88. ifstream cfile("log.conf");
  89. if(!cfile) { cout << "Failed to open config file." << endl; return 1; }
  90.  
  91. conf["iface"] = "eth0";
  92.  
  93. string line;
  94. while(cfile.good()) {
  95. getline(cfile, line);
  96. if(line.find(":") != line.npos) {
  97. string key = line.substr(0, line.find(":"));
  98. string val = line.substr(line.find(":")+1, (line.length() - line.find(":")));
  99. conf[key] = val;
  100. if(verbose) cout << key << " :: " << conf[key] << endl;
  101. }
  102. }
  103.  
  104. if(conf.find("host") == conf.end() ||
  105. conf.find("user") == conf.end() ||
  106. conf.find("pass") == conf.end() ||
  107. conf.find("db") == conf.end()) {
  108. cout << "Config file incomplete." << endl;
  109. return 1;
  110. }
  111. }
  112.  
  113. int mysqlInit(string db, string host, string user, string pass)
  114. {
  115. if(verbose) cout << "Connectng..." << endl;
  116. driver = sql::mysql::get_mysql_driver_instance();
  117. string th = "tcp://";
  118. th.append(host);
  119. con = driver->connect(th, user, pass);
  120.  
  121. stmt = con->createStatement();
  122. stmt->execute("USE cnl");
  123. }
  124.  
  125. int doQuery(string q)
  126. {
  127. if(verbose) cout << "Query:" << endl << q << endl;
  128. if(reallyquery) stmt->execute(q);
  129. query.str("");
  130. }
  131.  
  132. int main(int argc, char* argv[])
  133. {
  134. if(argc > 1) verbose = true;
  135.  
  136. readConf();
  137. mysqlInit(conf["db"].c_str(), conf["host"].c_str(), conf["user"].c_str(), conf["pass"].c_str());
  138.  
  139. // gets address of eth0 only, hard-coded atm///////////////////////////
  140. string ifacecmd = "ip addr show ";
  141. ifacecmd.append(conf["iface"]);
  142. map< int, map<int, string> > netinfo = doCmd(ifacecmd);
  143. string myIP = netinfo[3][1].substr(0, netinfo[3][1].find("/"));
  144. string subnet = netinfo[3][1].substr(netinfo[3][1].find("/") + 1, netinfo[3][1].size());
  145.  
  146. //list of hard drives/////////////////////////////////////////////////////////
  147. map< int, map<int, string> > drives = doCmd("ls -w 1 --color=never /dev/sd?");
  148.  
  149. //if(myIP.find(":") != myIP.npos) myIP.erase(0, myIP.find(":")+1);
  150. if(verbose) cout << myIP << endl;
  151.  
  152. //begin mem info////////////////////////////////////////////////////////////////////////////////////////
  153. map< int, map<int, string> > meminfo = doCmd("cat /proc/meminfo");
  154. map<string, string> memoutput;
  155. for(int l = 1; l < meminfo.size(); l++)
  156. {
  157. string name = meminfo[l][0];
  158. string val = meminfo[l][1];
  159. if(name.find(":") != name.npos) name.erase(name.find(":"), 1);
  160. memoutput[name] = val;
  161. //cout << "memoutput[" << name << "] = " << val << endl;
  162. }
  163.  
  164. if(verbose) cout << "---" << endl;
  165. query << "INSERT INTO cnl_log_memory ( server_ip, subnet, time, totalMem, memFree, buffers, cache, swapCache, swapFree, totalSwap, active_anon, inactive_anon, " <<
  166. "active_file, inactive_file ) VALUES ('" << myIP << "', '" << subnet << "', '" << time(NULL) << "', '" << memoutput["MemTotal"] << "', '" <<
  167. memoutput["MemFree"] << "', '" << memoutput["Buffers"] << "', '" << memoutput["Cached"] << "', '" << memoutput["SwapCached"] << "', '"
  168. << memoutput["SwapFree"] << "', '" << memoutput["SwapTotal"] << "', '" << memoutput["Active(anon)"] << "', '" << memoutput["Inactive(anon)"]
  169. << "', '" << memoutput["Active(file)"] << "', '" << memoutput["Inactive(file)"] << "')";
  170. doQuery(query.str()); query.str("");
  171.  
  172. //begin CPU info//////////////////////////////////////////////////////////////////////////////////////////
  173. map< int, map<int, string> > ut = doCmd("uptime");
  174.  
  175. string avg1;
  176. string avg2;
  177. string avg3;
  178. string uptime;
  179. if(ut[1][3].find("min") != ut[1][3].npos) { // up 28 min, etc. etc.
  180. avg1 = ut[1][8];
  181. if(avg1.find(",") != avg1.npos) avg1.erase(avg1.find(","));
  182. avg2 = ut[1][9];
  183. if(avg2.find(",") != avg2.npos) avg2.erase(avg2.find(","));
  184. avg3 = ut[1][10];
  185. uptime = ut[1][2];
  186. uptime.erase(0,1);
  187. uptime.insert(0, "00:");
  188. }
  189. else if(ut[1][3].find("day") != ut[1][3].npos){ // up 4 days, 2:52, etc. etc.
  190. avg1 = ut[1][9];
  191. if(avg1.find(",") != avg1.npos) avg1.erase(avg1.find(","));
  192. avg2 = ut[1][10];
  193. if(avg2.find(",") != avg2.npos) avg2.erase(avg2.find(","));
  194. avg3 = ut[1][12];
  195. uptime = ut[1][2];
  196. uptime.append(ut[1][3]);
  197. if(ut[1][4].find(",") != ut[1][4].npos) ut[1][4].erase(ut[1][4].find(","));
  198. uptime.append(ut[1][4]);
  199. }
  200. else { // up 13:24, etc.
  201. avg1 = ut[1][7];
  202. if(avg1.find(",") != avg1.npos) avg1.erase(avg1.find(","));
  203. avg2 = ut[1][8];
  204. if(avg2.find(",") != avg2.npos) avg2.erase(avg2.find(","));
  205. avg3 = ut[1][9];
  206. uptime = ut[1][2];
  207. uptime.erase(uptime.length() - 1);
  208. }
  209.  
  210. // iosdrives[iteration][drvname][word]
  211. map<int, map<string, map<int, string> > > iosdrives;
  212. //////////////////////iostat/////////////////////////////////////////////////
  213. for(int i = 1; i < 3; i++)
  214. {
  215. map< int, map<int, string> > ios = doCmd("iostat");
  216. string user = ios[4][0];
  217. string nice = ios[4][1];
  218. string system = ios[4][2];
  219. string iowait = ios[4][3];
  220. string steal = ios[4][4];
  221. string idle = ios[4][5];
  222.  
  223. query << "INSERT INTO cnl_log_cpu (server_ip, subnet, time, user, nice, system, iowait, steal, idle, ldAvg1, ldAvg2, ldAvg3, uptime) VALUES ('" <<
  224. myIP << "', '" << subnet <<"', '"<< time(NULL) <<"', '"<< user <<"', '"<< nice <<"', '"<< system <<"', '"<< iowait <<"', '"
  225. << steal <<"', '"<< idle <<"', '"<< avg1 <<"', '"<< avg2 <<"', '"<< avg3 <<"', '"<< uptime << "')";
  226. doQuery(query.str()); query.str("");
  227.  
  228. for(int it = 7; it < ios.size() - 1; it++)
  229. {
  230. //cout << ":: " << ios[it][0] << endl;
  231. for(int wrd = 0; wrd < ios[it].size(); wrd++)
  232. {
  233. iosdrives[i][ios[it][0]][wrd] = ios[it][wrd];
  234. //cout << "iosdrives[" << i << "][" << ios[it][0] << "][" << wrd << "] = " << ios[it][wrd] << endl;
  235. }
  236. }
  237. if(i == 1) { sleep(5); }
  238. //if(i == 2);
  239. }
  240.  
  241. //begin net info////////////////////////////////////////////////////////////////////////////////////////////////////
  242. map< int, map<int, string> > netdev = doCmd("cat /proc/net/dev", true);
  243. for(int i = netdev.size() - 3; i > 0; i--) {
  244. int c = i+2;
  245. string iface = netdev[c][0];
  246. if(iface.find(":") != iface.npos) iface.erase(iface.find(":"));
  247. string txBytes = netdev[c][9];
  248. string txPack = netdev[c][10];
  249. string txErrors = netdev[c][11];
  250. string txDrops = netdev[c][12];
  251. string rxBytes = netdev[c][1];
  252. string rxPack = netdev[c][2];
  253. string rxErrors = netdev[c][3];
  254. string rxDrops = netdev[c][4];
  255.  
  256. query << "INSERT INTO cnl_log_network ( server_ip, subnet, time, interface, sendBytes, sendPackets, sendErrors, sendDrops, rcvBytes, rcvPackets, rcvErrors, rcvDrops) VALUES ('"
  257. << myIP << "', '" << subnet << "', '" << time(NULL) << "', '" << iface << "', '" << txBytes << "', '" << txPack << "', '" << txErrors << "', '" << txDrops
  258. << "', '" << rxBytes << "', '" << rxPack << "', '" << rxErrors << "', '" << rxDrops << "')";
  259.  
  260. doQuery(query.str()); query.str("");
  261. }
  262.  
  263. //begin disk space info (df)/////////////////////////////////////////////////////////////////////////////////////////////
  264. map< int, map<int, string> > df = doCmd("df -B 1M");
  265. for(int li = 1; li < df.size(); li++)
  266. {
  267. if(li > 1)
  268. {
  269. if(df[li][0].find("none") == df[li][0].npos && df[li][0].find("/") != df[li][0].npos) {
  270. query << "INSERT INTO cnl_log_drives ( server_ip, subnet, time, drvName, totalSpace, spaceAvailable ) VALUES ('" << myIP << "', '" << subnet << "', '"
  271. << time(NULL) << "', '" << df[li][0] << "', '" << df[li][1] << "', '" << df[li][3] << "')";
  272. doQuery(query.str()); query.str("");
  273. }
  274. }
  275. }
  276.  
  277. //begin disk status info (smartctl)//////////////////////////////////////////////////////////////////////////////////////
  278. for(int i = 1; i < drives.size(); i++) //iterated for each drive//////////////////////
  279. {
  280. map< string, string > output;
  281. string c = string("/usr/sbin/smartctl -A ");
  282. c.append(drives[i][0]);
  283. //cout << drives[i][0] << endl;
  284. map< int, map<int, string> > curdr = doCmd(c);
  285. if(curdr[4][2].find("USB") != curdr[4][2].npos) {
  286. if(verbose) cout << "Drive " << curdr[4][0] << " is probably a USB stick/SD card/something. Not S.M.A.R.T.'d." << endl;
  287. }
  288. else
  289. {
  290. for(int ii = 7; ii < curdr.size() + 1; ii++)
  291. {
  292. /*if(curdr[ii][1].find("Raw_Read_Error_Rate") != curdr[ii][1].npos ||
  293. curdr[ii][1].find("Spin_Up_Time") != curdr[ii][1].npos ||
  294. curdr[ii][1].find("___") != curdr[ii][1].npos ||
  295. curdr[ii][1].find("Seek_Error_Rate") != curdr[ii][1].npos ||
  296. curdr[ii][1].find("___") != curdr[ii][1].npos ||
  297. curdr[ii][1].find("Power_Cycle_Count") != curdr[ii][1].npos ||
  298. curdr[ii][1].find("Temperature_Celsius") != curdr[ii][1].npos ||
  299. curdr[ii][1].find("Power_On_Hours") != curdr[ii][1].npos ||
  300. curdr[ii][1].find("Start_Stop_Count") != curdr[ii][1].npos ||
  301. curdr[ii][1].find("Reallocated_Sector_Ct") != curdr[ii][1].npos ||
  302. curdr[ii][1].find("Reallocated_Event_Count") != curdr[ii][1].npos ||
  303. curdr[ii][1].find("UDMA_CRC_Error_Count") != curdr[ii][1].npos)
  304. */
  305. // ^^ lol, figured I didn't really need all that
  306. for(int ii = 1; ii < curdr.size(); ii++)
  307. {
  308. string name = curdr[ii][1];
  309. if(name.find(" ") != name.npos) name.erase(name.find(" "),1);
  310. output[name] = curdr[ii][3];
  311. //cout << "output[" << name << "] : " << output[name] << endl;
  312. }
  313. }
  314. string drname = drives[i][0].substr(drives[i][0].find("/", 1) + 1, (drives[i][0].size() - drives[i][0].find("/", 1)));
  315. //cout << drname << endl;
  316. query << "INSERT INTO cnl_log_drives (server_ip, subnet, time, io, blkReadSec, BlkWriteSec, blkRead, blkWrites, drvName, readErrorRate, SpinUpTime, SeekErrorRate, " <<
  317. "PowerCycleCount, Temperature, powerOnHrs, StartStopCount, ReallocatedSectors, ReallocateEvents, CRCErrorCount ) VALUES ('"
  318. << myIP << "', '" << subnet << "', '" << time(NULL) << "', '"
  319. << iosdrives[1][drname][1] << "', '" << iosdrives[1][drname][2] << "', '" << iosdrives[1][drname][3] << "', '"
  320. << iosdrives[1][drname][4] << "', '" << iosdrives[1][drname][5] << "', '"
  321. << drives[i][0] << "', '" << output["Raw_Read_Error_Rate"] << "', '" << output["Spin_Up_Time"] << "', '" <<
  322. output["Seek_Error_Rate"] << "', '" << output["Power_Cycle_Count"] << "', '" <<
  323. output["Temperature_Celsius"] << "', '" << output["Power_On_Hours"] << "', '" <<
  324. output["Start_Stop_Count"] << "', '" << output["Reallocated_Sector_Ct"] << "', '" <<
  325. output["Reallocated_Event_Count"] << "', '" << output["UDMA_CRC_Error_Count"] << "')";
  326. doQuery(query.str()); query.str("");
  327. }
  328. }
  329.  
  330. delete stmt;
  331. delete con;
  332.  
  333. return 0;
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement