Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.02 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. string memTotal = meminfo[1][1];
  140. string memFree = meminfo[2][1];
  141. string buffers = meminfo[3][1];
  142. string cache = meminfo[4][1];
  143. string swapCached = meminfo[5][1];
  144. string swapFree = meminfo[15][1];
  145. string swapTotal = meminfo[14][1];
  146. string activeAnon = meminfo[7][1];
  147. string inactiveAnon = meminfo[8][1];
  148. string activeFile = meminfo[9][1];
  149. string inactiveFile = meminfo[10][1];
  150.  
  151. if(verbose) cout << "---" << endl;
  152. query << "INSERT INTO cnl_log_memory ( server_ip, time, totalMem, memFree, buffers, cache, swapCache, swapFree, totalSwap, active_anon, inactive_anon, " <<
  153. "active_file, inactive_file ) VALUES ('" << myIP << "', '" << time(NULL) << "', '" << memTotal << "', '" <<
  154. memFree << "', '" << buffers << "', '" << cache << "', '" << swapCached << "', '" << swapFree << "', '" <<
  155. swapTotal << "', '" << activeAnon << "', '" << inactiveAnon << "', '" << activeFile << "', '" << inactiveFile << "')";
  156. doQuery(query.str()); query.str("");
  157.  
  158. //begin CPU info//////////////////////////////////////////////////////////////////////////////////////////
  159. map< int, map<int, string> > ios = doCmd("iostat 5 2");
  160. string user = ios[4][0];
  161. string nice = ios[4][1];
  162. string system = ios[4][2];
  163. string iowait = ios[4][3];
  164. string steal = ios[4][4];
  165. string idle = ios[4][5];
  166.  
  167. map< int, map<int, string> > ut = doCmd("uptime");
  168.  
  169. string avg1;
  170. string avg2;
  171. string avg3;
  172. string uptime;
  173. if(ut[1][3].find("min") != ut[1][3].npos) { // up 28 min, etc. etc.
  174. avg1 = ut[1][8];
  175. if(avg1.find(",") != avg1.npos) avg1.erase(avg1.find(","));
  176. avg2 = ut[1][9];
  177. if(avg2.find(",") != avg2.npos) avg2.erase(avg2.find(","));
  178. avg3 = ut[1][10];
  179. uptime = ut[1][2];
  180. uptime.erase(0,1);
  181. uptime.insert(0, "00:");
  182. }
  183. else if(ut[1][3].find("day") != ut[1][3].npos){ // up 4 days, 2:52, etc. etc.
  184. avg1 = ut[1][9];
  185. if(avg1.find(",") != avg1.npos) avg1.erase(avg1.find(","));
  186. avg2 = ut[1][10];
  187. if(avg2.find(",") != avg2.npos) avg2.erase(avg2.find(","));
  188. avg3 = ut[1][12];
  189. uptime = ut[1][2];
  190. uptime.append(ut[1][3]);
  191. if(ut[1][4].find(",") != ut[1][4].npos) ut[1][4].erase(ut[1][4].find(","));
  192. uptime.append(ut[1][4]);
  193. }
  194. else { // up 13:24, etc.
  195. avg1 = ut[1][7];
  196. if(avg1.find(",") != avg1.npos) avg1.erase(avg1.find(","));
  197. avg2 = ut[1][8];
  198. if(avg2.find(",") != avg2.npos) avg2.erase(avg2.find(","));
  199. avg3 = ut[1][9];
  200. uptime = ut[1][2];
  201. uptime.erase(uptime.length() - 1);
  202. }
  203.  
  204. query << "INSERT INTO cnl_log_cpu (server_ip, time, user, nice, system, iowait, steal, idle, ldAvg1, ldAvg2, ldAvg3, uptime) VALUES ('" <<
  205. myIP <<"', '"<< time(NULL) <<"', '"<< user <<"', '"<< nice <<"', '"<< system <<"', '"<< iowait <<"', '"
  206. << steal <<"', '"<< idle <<"', '"<< avg1 <<"', '"<< avg2 <<"', '"<< avg3 <<"', '"<< uptime << "')";
  207. doQuery(query.str()); query.str("");
  208.  
  209.  
  210. user = ios[11][0];
  211. nice = ios[11][1];
  212. system = ios[11][2];
  213. iowait = ios[11][3];
  214. steal = ios[11][4];
  215. idle = ios[11][5];
  216.  
  217. query << "INSERT INTO cnl_log_cpu (server_ip, time, user, nice, system, iowait, steal, idle, ldAvg1, ldAvg2, ldAvg3, uptime) VALUES ('" <<
  218. myIP <<"', '"<< time(NULL) <<"', '"<< user <<"', '"<< nice <<"', '"<< system <<"', '"<< iowait <<"', '"
  219. << steal <<"', '"<< idle <<"', '"<< avg1 <<"', '"<< avg2 <<"', '"<< avg3 <<"', '"<< uptime << "')";
  220. doQuery(query.str()); query.str("");
  221.  
  222. //begin net info////////////////////////////////////////////////////////////////////////////////////////////////////
  223. map< int, map<int, string> > netdev = doCmd("cat /proc/net/dev");
  224. for(int i = netdev.size() - 3; i > 0; i--) {
  225. int c = i+2;
  226. string iface = netdev[c][0];
  227. if(iface.find(":") != iface.npos) iface.erase(iface.find(":"));
  228. string txBytes = netdev[c][9];
  229. string txPack = netdev[c][10];
  230. string txErrors = netdev[c][11];
  231. string txDrops = netdev[c][12];
  232. string rxBytes = netdev[c][1];
  233. string rxPack = netdev[c][2];
  234. string rxErrors = netdev[c][3];
  235. string rxDrops = netdev[c][4];
  236.  
  237. query << "INSERT INTO cnl_log_network ( server_ip, time, interface, sendBytes, sendPackets, sendErrors, sendDrops, rcvBytes, rcvPackets, rcvErrors, rcvDrops) VALUES ('"
  238. << myIP << "', '" << time(NULL) << "', '" << iface << "', '" << txBytes << "', '" << txPack << "', '" << txErrors << "', '" << txDrops
  239. << "', '" << rxBytes << "', '" << rxPack << "', '" << rxErrors << "', '" << rxDrops << "')";
  240.  
  241. doQuery(query.str()); query.str("");
  242. }
  243.  
  244. //begin disk space info (df)/////////////////////////////////////////////////////////////////////////////////////////////
  245. map< int, map<int, string> > df = doCmd("df -B 1M");
  246. for(int li = 1; li < df.size(); li++)
  247. {
  248. if(li > 1)
  249. {
  250. if(df[li][0].find("none") == df[li][0].npos) {
  251. query << "INSERT INTO cnl_log_drives ( server_ip, time, drvName, totalSpace, spaceAvailable ) VALUES ('" << myIP << "', '"
  252. << time(NULL) << "', '" << df[li][0] << "', '" << df[li][1] << "', '" << df[li][3] << "')";
  253. doQuery(query.str()); query.str("");
  254. }
  255. }
  256. }
  257.  
  258. //begin disk status info (smartctl)//////////////////////////////////////////////////////////////////////////////////////
  259. map< int, map<int, string> > drives = doCmd("ls -w 1 --color=never /dev/sd?");
  260. for(int i = 1; i < drives.size(); i++) //iterated for each drive//////////////////////
  261. {
  262. map< string, string > output;
  263. string c = string("smartctl --attributes ");
  264. c.append(drives[i][0]);
  265. //cout << drives[i][0] << endl;
  266. map< int, map<int, string> > curdr = doCmd(c);
  267. if(curdr[4][2].find("USB") != curdr[4][2].npos) {
  268. if(verbose) cout << "Drive " << curdr[4][0] << " is probably a USB stick/SD card/something. Not S.M.A.R.T.'d." << endl;
  269. }
  270. else
  271. {
  272. for(int ii = 7; ii < curdr.size() + 1; ii++)
  273. {
  274. /*if(curdr[ii][1].find("Raw_Read_Error_Rate") != curdr[ii][1].npos ||
  275. curdr[ii][1].find("Spin_Up_Time") != curdr[ii][1].npos ||
  276. curdr[ii][1].find("___") != curdr[ii][1].npos ||
  277. curdr[ii][1].find("Seek_Error_Rate") != curdr[ii][1].npos ||
  278. curdr[ii][1].find("___") != curdr[ii][1].npos ||
  279. curdr[ii][1].find("Power_Cycle_Count") != curdr[ii][1].npos ||
  280. curdr[ii][1].find("Temperature_Celsius") != curdr[ii][1].npos ||
  281. curdr[ii][1].find("Power_On_Hours") != curdr[ii][1].npos ||
  282. curdr[ii][1].find("Start_Stop_Count") != curdr[ii][1].npos ||
  283. curdr[ii][1].find("Reallocated_Sector_Ct") != curdr[ii][1].npos ||
  284. curdr[ii][1].find("Reallocated_Event_Count") != curdr[ii][1].npos ||
  285. curdr[ii][1].find("UDMA_CRC_Error_Count") != curdr[ii][1].npos)
  286. */
  287. // ^^ lol, figured I didn't really need all that
  288. for(int ii = 1; ii < curdr.size(); ii++)
  289. {
  290. string name = curdr[ii][1];
  291. if(name.find(" ") != name.npos) name.erase(name.find(" "),1);
  292. output[name] = curdr[ii][3];
  293. //cout << "output[" << name << "] : " << output[name] << endl;
  294. }
  295. }
  296. query << "INSERT INTO cnl_log_drives (server_ip, time, drvName, readErrorRate, SpinUpTime, SeekErrorRate, " <<
  297. "PowerCycleCount, Temperature, powerOnHrs, StartStopCount, ReallocatedSectors, ReallocateEvents, CRCErrorCount ) VALUES ('"
  298. << myIP << "', '" << time(NULL) << "', '"
  299. << drives[i][0] << "', '" << output["Raw_Read_Error_Rate"] << "', '" << output["Spin_Up_Time"] << "', '" <<
  300. output["Seek_Error_Rate"] << "', '" << output["Power_Cycle_Count"] << "', '" <<
  301. output["Temperature_Celsius"] << "', '" << output["Power_On_Hours"] << "', '" <<
  302. output["Start_Stop_Count"] << "', '" << output["Reallocated_Sector_Ct"] << "', '" <<
  303. output["Reallocated_Event_Count"] << "', '" << output["UDMA_CRC_Error_Count"] << "')";
  304. doQuery(query.str()); query.str("");
  305. }
  306. }
  307.  
  308. delete stmt;
  309. delete con;
  310.  
  311. return 0;
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement