Advertisement
Guest User

Untitled

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