Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string>
- #include <sstream>
- #include <map>
- #include <mysql_driver.h>
- #include <mysql_connection.h>
- #include <cppconn/statement.h>
- using namespace std;
- bool verbose = false;
- bool reallyquery = false;
- fstream tfile;
- map<string, string> conf;
- stringstream query;
- sql::mysql::MySQL_Driver *driver;
- sql::Connection *con;
- sql::Statement *stmt;
- map< int, map<int, string> > readFile(string name)
- {
- ifstream whatfile(name.c_str());
- if(whatfile.fail()) cout << "Failed.";
- string line;
- int li = 1;
- map< int, map<int, string> > themap;
- while(whatfile.good()) {
- getline(whatfile, line);
- //cout << line << endl;
- for(int i = 0; line.find(" ") != line.npos; )
- {
- i = line.find(" ");
- line.replace(i, 2, " ");
- }
- if(line.find(" ") == line.npos)
- {
- themap[li][0] = line;
- line = "";
- //cout << "+++ " << themap[li][0]<<endl;
- }
- else
- {
- for(int i = 0; !line.empty(); i++)
- {
- if(line.find(" ") == line.npos) break;
- //cout << line << endl;
- int s = line.find(" ");
- if(i == 0) s = 0;
- int e = line.find(" ", s + 1);
- themap[li][i] = line.substr(s, e - s);
- //cout << "themap[" << li << "][" << i << "] = " << themap[li][i] << endl;
- if(!line.empty()) line.erase(s, e - s);
- }
- }
- li++;
- }
- whatfile.close();
- //cout << themap[2][2] << endl;
- return themap;
- }
- map< int, map<int, string> > doCmd(string cmd)
- {
- cmd.append(" > .logtmp");
- system(cmd.c_str());
- map< int, map<int, string> > out = readFile(".logtmp");
- system("rm .logtmp");
- return out;
- }
- int readConf()
- {
- ifstream cfile("log.conf");
- if(!cfile) { cout << "Failed to open config file." << endl; return 1; }
- string line;
- while(cfile.good()) {
- getline(cfile, line);
- if(line.find(":") != line.npos) {
- string key = line.substr(0, line.find(":"));
- string val = line.substr(line.find(":")+1, (line.length() - line.find(":")));
- conf[key] = val;
- if(verbose) cout << key << " :: " << conf[key] << endl;
- }
- }
- if(conf.find("host") == conf.end() ||
- conf.find("user") == conf.end() ||
- conf.find("pass") == conf.end() ||
- conf.find("db") == conf.end()) {
- cout << "Config file incomplete." << endl;
- return 1;
- }
- }
- int mysqlInit(string db, string host, string user, string pass)
- {
- driver = sql::mysql::get_mysql_driver_instance();
- string th = "tcp://";
- th.append(host);
- con = driver->connect(th, user, pass);
- stmt = con->createStatement();
- stmt->execute("USE cnl");
- }
- int doQuery(string q)
- {
- if(verbose) cout << "Query:" << endl << q << endl;
- if(reallyquery) stmt->execute(q);
- query.str("");
- }
- int main(int argc, char* argv[])
- {
- if(argc > 1) verbose = true;
- readConf();
- mysqlInit(conf["db"].c_str(), conf["host"].c_str(), conf["user"].c_str(), conf["pass"].c_str());
- if(verbose) cout << "Connectng..." << endl;
- //mysqlpp::Connection conn(false);
- map< int, map<int, string> > netinfo = doCmd("ifconfig eth0");
- string myIP = netinfo[2][1];
- cout << myIP << endl;
- if(myIP.find(":") != myIP.npos) myIP.erase(0, myIP.find(":")+1);
- if(verbose) cout << myIP << endl;
- //begin mem info////////////////////////////////////////////////////////////////////////////////////////
- map< int, map<int, string> > meminfo = doCmd("cat /proc/meminfo");
- map<string, string> memoutput;
- for(int l = 1; l < meminfo.size(); l++)
- {
- string name = meminfo[l][0];
- string val = meminfo[l][1];
- if(name.find(":") != name.npos) name.erase(name.find(":"), 1);
- memoutput[name] = val;
- cout << "memoutput[" << name << "] = " << val << endl;
- }
- if(verbose) cout << "---" << endl;
- query << "INSERT INTO cnl_log_memory ( server_ip, time, totalMem, memFree, buffers, cache, swapCache, swapFree, totalSwap, active_anon, inactive_anon, " <<
- "active_file, inactive_file ) VALUES ('" << myIP << "', '" << time(NULL) << "', '" << memoutput["MemTotal"] << "', '" <<
- memoutput["MemFree"] << "', '" << memoutput["Buffers"] << "', '" << memoutput["Cached"] << "', '" << memoutput["SwapCached"] << "', '"
- << memoutput["SwapFree"] << "', '" << memoutput["SwapTotal"] << "', '" << memoutput["Active(anon)"] << "', '" << memoutput["Inactive(anon)"]
- << "', '" << memoutput["Active(file)"] << "', '" << memoutput["Inactive(file)"] << "')";
- doQuery(query.str()); query.str("");
- //begin CPU info//////////////////////////////////////////////////////////////////////////////////////////
- map< int, map<int, string> > ios = doCmd("iostat 5 2");
- string user = ios[4][0];
- string nice = ios[4][1];
- string system = ios[4][2];
- string iowait = ios[4][3];
- string steal = ios[4][4];
- string idle = ios[4][5];
- map< int, map<int, string> > ut = doCmd("uptime");
- string avg1;
- string avg2;
- string avg3;
- string uptime;
- if(ut[1][3].find("min") != ut[1][3].npos) { // up 28 min, etc. etc.
- avg1 = ut[1][8];
- if(avg1.find(",") != avg1.npos) avg1.erase(avg1.find(","));
- avg2 = ut[1][9];
- if(avg2.find(",") != avg2.npos) avg2.erase(avg2.find(","));
- avg3 = ut[1][10];
- uptime = ut[1][2];
- uptime.erase(0,1);
- uptime.insert(0, "00:");
- }
- else if(ut[1][3].find("day") != ut[1][3].npos){ // up 4 days, 2:52, etc. etc.
- avg1 = ut[1][9];
- if(avg1.find(",") != avg1.npos) avg1.erase(avg1.find(","));
- avg2 = ut[1][10];
- if(avg2.find(",") != avg2.npos) avg2.erase(avg2.find(","));
- avg3 = ut[1][12];
- uptime = ut[1][2];
- uptime.append(ut[1][3]);
- if(ut[1][4].find(",") != ut[1][4].npos) ut[1][4].erase(ut[1][4].find(","));
- uptime.append(ut[1][4]);
- }
- else { // up 13:24, etc.
- avg1 = ut[1][7];
- if(avg1.find(",") != avg1.npos) avg1.erase(avg1.find(","));
- avg2 = ut[1][8];
- if(avg2.find(",") != avg2.npos) avg2.erase(avg2.find(","));
- avg3 = ut[1][9];
- uptime = ut[1][2];
- uptime.erase(uptime.length() - 1);
- }
- query << "INSERT INTO cnl_log_cpu (server_ip, time, user, nice, system, iowait, steal, idle, ldAvg1, ldAvg2, ldAvg3, uptime) VALUES ('" <<
- myIP <<"', '"<< time(NULL) <<"', '"<< user <<"', '"<< nice <<"', '"<< system <<"', '"<< iowait <<"', '"
- << steal <<"', '"<< idle <<"', '"<< avg1 <<"', '"<< avg2 <<"', '"<< avg3 <<"', '"<< uptime << "')";
- doQuery(query.str()); query.str("");
- user = ios[11][0];
- nice = ios[11][1];
- system = ios[11][2];
- iowait = ios[11][3];
- steal = ios[11][4];
- idle = ios[11][5];
- query << "INSERT INTO cnl_log_cpu (server_ip, time, user, nice, system, iowait, steal, idle, ldAvg1, ldAvg2, ldAvg3, uptime) VALUES ('" <<
- myIP <<"', '"<< time(NULL) <<"', '"<< user <<"', '"<< nice <<"', '"<< system <<"', '"<< iowait <<"', '"
- << steal <<"', '"<< idle <<"', '"<< avg1 <<"', '"<< avg2 <<"', '"<< avg3 <<"', '"<< uptime << "')";
- doQuery(query.str()); query.str("");
- //begin net info////////////////////////////////////////////////////////////////////////////////////////////////////
- map< int, map<int, string> > netdev = doCmd("cat /proc/net/dev");
- for(int i = netdev.size() - 3; i > 0; i--) {
- int c = i+2;
- string iface = netdev[c][0];
- if(iface.find(":") != iface.npos) iface.erase(iface.find(":"));
- string txBytes = netdev[c][9];
- string txPack = netdev[c][10];
- string txErrors = netdev[c][11];
- string txDrops = netdev[c][12];
- string rxBytes = netdev[c][1];
- string rxPack = netdev[c][2];
- string rxErrors = netdev[c][3];
- string rxDrops = netdev[c][4];
- query << "INSERT INTO cnl_log_network ( server_ip, time, interface, sendBytes, sendPackets, sendErrors, sendDrops, rcvBytes, rcvPackets, rcvErrors, rcvDrops) VALUES ('"
- << myIP << "', '" << time(NULL) << "', '" << iface << "', '" << txBytes << "', '" << txPack << "', '" << txErrors << "', '" << txDrops
- << "', '" << rxBytes << "', '" << rxPack << "', '" << rxErrors << "', '" << rxDrops << "')";
- doQuery(query.str()); query.str("");
- }
- //begin disk space info (df)/////////////////////////////////////////////////////////////////////////////////////////////
- map< int, map<int, string> > df = doCmd("df -B 1M");
- for(int li = 1; li < df.size(); li++)
- {
- if(li > 1)
- {
- if(df[li][0].find("none") == df[li][0].npos) {
- query << "INSERT INTO cnl_log_drives ( server_ip, time, drvName, totalSpace, spaceAvailable ) VALUES ('" << myIP << "', '"
- << time(NULL) << "', '" << df[li][0] << "', '" << df[li][1] << "', '" << df[li][3] << "')";
- doQuery(query.str()); query.str("");
- }
- }
- }
- //begin disk status info (smartctl)//////////////////////////////////////////////////////////////////////////////////////
- map< int, map<int, string> > drives = doCmd("ls -w 1 --color=never /dev/sd?");
- for(int i = 1; i < drives.size(); i++) //iterated for each drive//////////////////////
- {
- map< string, string > output;
- string c = string("smartctl --attributes ");
- c.append(drives[i][0]);
- //cout << drives[i][0] << endl;
- map< int, map<int, string> > curdr = doCmd(c);
- if(curdr[4][2].find("USB") != curdr[4][2].npos) {
- if(verbose) cout << "Drive " << curdr[4][0] << " is probably a USB stick/SD card/something. Not S.M.A.R.T.'d." << endl;
- }
- else
- {
- for(int ii = 7; ii < curdr.size() + 1; ii++)
- {
- /*if(curdr[ii][1].find("Raw_Read_Error_Rate") != curdr[ii][1].npos ||
- curdr[ii][1].find("Spin_Up_Time") != curdr[ii][1].npos ||
- curdr[ii][1].find("___") != curdr[ii][1].npos ||
- curdr[ii][1].find("Seek_Error_Rate") != curdr[ii][1].npos ||
- curdr[ii][1].find("___") != curdr[ii][1].npos ||
- curdr[ii][1].find("Power_Cycle_Count") != curdr[ii][1].npos ||
- curdr[ii][1].find("Temperature_Celsius") != curdr[ii][1].npos ||
- curdr[ii][1].find("Power_On_Hours") != curdr[ii][1].npos ||
- curdr[ii][1].find("Start_Stop_Count") != curdr[ii][1].npos ||
- curdr[ii][1].find("Reallocated_Sector_Ct") != curdr[ii][1].npos ||
- curdr[ii][1].find("Reallocated_Event_Count") != curdr[ii][1].npos ||
- curdr[ii][1].find("UDMA_CRC_Error_Count") != curdr[ii][1].npos)
- */
- // ^^ lol, figured I didn't really need all that
- for(int ii = 1; ii < curdr.size(); ii++)
- {
- string name = curdr[ii][1];
- if(name.find(" ") != name.npos) name.erase(name.find(" "),1);
- output[name] = curdr[ii][3];
- //cout << "output[" << name << "] : " << output[name] << endl;
- }
- }
- query << "INSERT INTO cnl_log_drives (server_ip, time, drvName, readErrorRate, SpinUpTime, SeekErrorRate, " <<
- "PowerCycleCount, Temperature, powerOnHrs, StartStopCount, ReallocatedSectors, ReallocateEvents, CRCErrorCount ) VALUES ('"
- << myIP << "', '" << time(NULL) << "', '"
- << drives[i][0] << "', '" << output["Raw_Read_Error_Rate"] << "', '" << output["Spin_Up_Time"] << "', '" <<
- output["Seek_Error_Rate"] << "', '" << output["Power_Cycle_Count"] << "', '" <<
- output["Temperature_Celsius"] << "', '" << output["Power_On_Hours"] << "', '" <<
- output["Start_Stop_Count"] << "', '" << output["Reallocated_Sector_Ct"] << "', '" <<
- output["Reallocated_Event_Count"] << "', '" << output["UDMA_CRC_Error_Count"] << "')";
- doQuery(query.str()); query.str("");
- }
- }
- delete stmt;
- delete con;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement