Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstring>
- #include <fstream>
- #include <sstream>
- #define c 29
- using namespace std;
- string fSplit(const string& fString, int which)
- {
- istringstream iss; string val;
- iss.str(fString);
- for (int i = 0; i < which; i++) {
- iss >> val;
- }return val;
- }
- int main()
- {
- int i = 0, j = 0, k = 0, linecount = 0, line = 0, pick = 0, skip = 0;
- string testline;
- string store[c];
- string node[c];
- string word[49];
- string temp;
- string cpus[c];
- string used[c];
- string mem[c];
- string state[c];
- string total[4];
- int cputot[5], usedtot[5], memtot[5];
- int jobcount = 0;
- string *storejobs;
- string *jobid;
- string *username;
- string *queue;
- string *jobname;
- string *ssid;
- string *nds;
- string *tsk;
- string *reqmem;
- string *reqtime;
- string *s;
- string *elaptime;
- string *nodelist;
- fstream Test;
- Test.open("pbs_output.txt", fstream::in);
- if (!Test)
- {
- cout << "There was a problem opening the file. Press any key to close.\n";
- getchar();
- return 0;
- }
- //store words in array
- while (getline(Test, testline))
- {
- if (testline != "")
- {
- word[i] = testline;
- i++;
- }
- }
- Test.close();
- for (int k = 0; k < i; k++)
- {
- if (word[k] == "Job ID Username Queue Jobname SessID NDS TSK Memory Time S Time") {
- k = k + 2;
- while (word[k] != "NODE CPUS USED MEM STATE") {
- jobcount++;
- k++;
- }
- }
- }
- storejobs = new string[jobcount];
- jobid = new string[jobcount];
- username = new string[jobcount];
- queue = new string[jobcount];
- jobname = new string[jobcount];
- ssid = new string[jobcount];
- nds = new string[jobcount];
- tsk = new string[jobcount];
- reqmem = new string[jobcount];
- reqtime = new string[jobcount];
- s = new string[jobcount];
- elaptime = new string[jobcount];
- nodelist = new string[jobcount];
- for (int k = 0; k < i; k++)
- {
- if (word[k] == "Job ID Username Queue Jobname SessID NDS TSK Memory Time S Time") {
- k = k + 2;
- while (word[k] != "NODE CPUS USED MEM STATE") {
- storejobs[j] = word[k];
- k++;
- j++;
- }
- }
- }
- for (k = 0; k < jobcount; k++)
- {
- jobid[k] = fSplit(storejobs[k], 1);
- username[k] = fSplit(storejobs[k], 2);
- queue[k] = fSplit(storejobs[k], 3);
- jobname[k] = fSplit(storejobs[k], 4);
- ssid[k] = fSplit(storejobs[k], 5);
- nds[k] = fSplit(storejobs[k], 6);
- tsk[k] = fSplit(storejobs[k], 7);
- reqmem[k] = fSplit(storejobs[k], 8);
- reqtime[k] = fSplit(storejobs[k], 9);
- s[k] = fSplit(storejobs[k], 10);
- elaptime[k] = fSplit(storejobs[k], 11);
- nodelist[k] = fSplit(storejobs[k], 12);
- //cout <<endl << jobid[k] << endl << queue[k] << endl << jobname[k] << endl << ssid[k] << endl << nds[k] << endl << tsk[k] << endl << reqmem[k] << endl << reqtime[k] << endl << elaptime[k] << endl << jobid[k] << endl << nodelist[k] << endl << endl;
- }
- for (int k = 0; k < i; k++)
- {
- if (word[k] == "NODE CPUS USED MEM STATE") {
- line++;
- if (line == 1)//occurence one
- {
- pick = 1;//line counter
- while (pick < 5) //take first five lines of following the first occurence
- {
- temp = word[k + pick];
- store[linecount] = temp;//store each line
- linecount++;
- pick++;
- }
- if (pick >= 5)
- {
- total[3] = word[k + pick + 1];
- }
- }
- else if (line == 2)
- {
- pick = 1;
- while (pick < 10)
- {
- temp = word[k + pick];
- store[linecount] = temp;
- linecount++;
- pick++;
- }
- if (pick >= 10)
- {
- total[0] = word[k + pick + 1];
- }
- }
- else if (line == 3)
- {
- pick = 1;
- while (pick < 11)
- {
- temp = word[k + pick];
- store[linecount] = temp;
- linecount++;
- pick++;
- }
- if (pick >= 11)
- {
- total[1] = word[k + pick + 1];
- }
- }
- else if (line == 4)
- {
- pick = 1;
- while (pick < 7)
- {
- temp = word[k + pick];
- store[linecount] = temp;
- linecount++;
- pick++;
- }
- if (pick >= 7)
- {
- total[2] = word[k + pick + 1];
- }
- }
- }
- }
- cout << linecount << endl;
- for (j = 0; j < linecount; j++)
- {
- node[j] = fSplit(store[j], 1);
- cpus[j] = fSplit(store[j], 2);
- used[j] = fSplit(store[j], 3);
- mem[j] = fSplit(store[j], 4);
- state[j] = fSplit(store[j], 5);
- }
- for (i = 0; i < 4; i++)
- {
- stringstream(fSplit(total[i], 2)) >> cputot[i];
- stringstream(fSplit(total[i], 3)) >> usedtot[i];
- stringstream(fSplit(total[i], 4)) >> memtot[i];
- }
- cputot[4] = cputot[0] + cputot[1] + cputot[2];
- usedtot[4] = usedtot[0] + usedtot[1] + usedtot[2];
- memtot[4] = memtot[0] + memtot[1] + memtot[2];
- //for (i = 0; i < linecount; i++) {
- //cout << node[i] << endl << cpus[i] << endl << used[i] << endl << mem[i] << endl << state[i] << endl;
- //}
- //cout << memtot[3]<<endl<< usedtot[3]<<endl<< cputot[3];
- fstream json;
- json.open("data.json", fstream::out);
- if (!json)
- {
- cout << "There was a problem opening the file. Press any key to close.\n";
- getchar();
- return 0;
- }
- json << "{" << endl << "\t\"jobs\": [" << endl << endl;
- for (i = 0; i < jobcount - 1; i++)
- {
- json << "\t\t{" << endl << endl << "\t\t\t\"jobid\": " << "\"" << jobid[i] << "\"," << endl;
- json << "\t\t\t\"username\": " << "\"" << username[i] << "\"," << endl;
- json << "\t\t\t\"queue\": " << "\"" << queue[i] << "\"," << endl;
- json << "\t\t\t\"jobname\": " << "\"" << jobname[i] << "\"," << endl;
- json << "\t\t\t\"ssid\": " << "\"" << ssid[i] << "\"," << endl;
- json << "\t\t\t\"nds\": " << "\"" << nds[i] << "\"," << endl;
- json << "\t\t\t\"tsk\": " << "\"" << tsk[i] << "\"," << endl;
- json << "\t\t\t\"reqmem\": " << "\"" << reqmem[i] << "\"," << endl;
- json << "\t\t\t\"reqtime\": " << "\"" << reqtime[i] << "\"," << endl;
- json << "\t\t\t\"s\": " << "\"" << s[i] << "\"," << endl;
- json << "\t\t\t\"elaptime\": " << "\"" << elaptime[i] << "\"," << endl;
- json << "\t\t\t\"nodelist\": " << "\"" << nodelist[i] << "\"" << endl << "\t\t}," << endl;
- }
- for (i = jobcount - 1; i < jobcount; i++)
- {
- json << "\t\t{" << endl << endl << "\t\t\t\"jobid\": " << "\"" << jobid[i] << "\"," << endl;
- json << "\t\t\t\"username\": " << "\"" << username[i] << "\"," << endl;
- json << "\t\t\t\"queue\": " << "\"" << queue[i] << "\"," << endl;
- json << "\t\t\t\"jobname\": " << "\"" << jobname[i] << "\"," << endl;
- json << "\t\t\t\"ssid\": " << "\"" << ssid[i] << "\"," << endl;
- json << "\t\t\t\"nds\": " << "\"" << nds[i] << "\"," << endl;
- json << "\t\t\t\"tsk\": " << "\"" << tsk[i] << "\"," << endl;
- json << "\t\t\t\"reqmem\": " << "\"" << reqmem[i] << "\"," << endl;
- json << "\t\t\t\"reqtime\": " << "\"" << reqtime[i] << "\"," << endl;
- json << "\t\t\t\"s\": " << "\"" << s[i] << "\"," << endl;
- json << "\t\t\t\"elaptime\": " << "\"" << elaptime[i] << "\"," << endl;
- json << "\t\t\t\"nodelist\": " << "\"" << nodelist[i] << "\"" << endl << "\t\t}" << endl << "],";
- }
- json << endl << "\t\"nodes\": [" << endl << endl;
- for (i = 0; i < 28; i++)
- {
- json << "\t\t{" << endl << endl << "\t\t\t\"NODE\": " << "\"" << node[i] << "\"," << endl;
- json << "\t\t\t\"CPU\": " << "\"" << cpus[i] << "\"," << endl;
- json << "\t\t\t\"USED\": " << "\"" << used[i] << "\"," << endl;
- json << "\t\t\t\"MEM\": " << "\"" << mem[i] << "\"," << endl;
- json << "\t\t\t\"STATE\": " << "\"" << state[i] << "\"" << endl << "\t\t}," << endl;
- }
- for (i = 28; i < 29; i++)
- {
- json << "\t\t{" << endl << endl << "\t\t\t\"NODE\": " << "\"" << node[i] << "\"," << endl;
- json << "\t\t\t\"CPU\": " << "\"" << cpus[i] << "\"," << endl;
- json << "\t\t\t\"USED\": " << "\"" << used[i] << "\"," << endl;
- json << "\t\t\t\"MEM\": " << "\"" << mem[i] << "\"," << endl;
- json << "\t\t\t\"STATE\": " << "\"" << state[i] << "\"" << endl << "\t\t}" << endl << "]," << endl;
- }
- json << endl << "\t\"node9tot\": [" << endl << endl;
- json << "\t\t{" << endl << endl << "\t\t\t\"cputot\": " << "\"" << cputot[3] << "\"," << endl;
- json << "\t\t\t\"usedtot\": " << "\"" << usedtot[3] << "\"," << endl;
- json << "\t\t\t\"memtot\": " << "\"" << memtot[3] << "\"" << endl << "\t\t}" << endl << "],"<<endl;
- json << endl << "\t\"node0-2tot\": [" << endl << endl;
- json << "\t\t{" << endl << endl << "\t\t\t\"cputot\": " << "\"" << cputot[4] << "\"," << endl;
- json << "\t\t\t\"usedtot\": " << "\"" << usedtot[4] << "\"," << endl;
- json << "\t\t\t\"memtot\": " << "\"" << memtot[4] << "\"" << endl << "\t\t}" << endl << "]" << endl << "}";
- json.close();
- delete[]storejobs;
- system("pause"); return 0;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement