Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSystemToolkit.hh
Go to the documentation of this file.
1 #ifndef __JSYSTEMTOOLKIT__
2 #define __JSYSTEMTOOLKIT__
3 
4 #include <iostream>
5 #include <string>
6 #include <vector>
7 #include <unistd.h>
8 
9 #include "JLang/JException.hh"
10 #include "JSystem/JShell.hh"
11 #include "JSystem/JSysinfo.hh"
12 
13 
14 /**
15  * \file
16  * System calls via shell interactions.
17  * \author mdejong
18  */
19 /**
20  * Auxiliary classes and methods for operating system calls.
21  */
22 namespace JSYSTEM {}
23 namespace JPP { using namespace JSYSTEM; }
24 
25 namespace JSYSTEM {
26 
28 
29  /**
30  * Shell names.
31  */
32  static const char* const ZSH = "zsh";
33  static const char* const KSH = "ksh";
34  static const char* const BASH = "bash";
35  static const char* const CSH = "csh";
36  static const char* const TCSH = "tcsh";
37 
38 
39  /**
40  * Get memory usage in percent of given process identifier.
41  *
42  * \param shell shell interface
43  * \param pid process identifier
44  * \return memory usage [%]
45  */
46  inline float getMemoryUsage(JShell& shell, const pid_t pid)
47  {
48  using namespace std;
49 
50  float value = 0.0;
51 
52  shell << "ps -o %mem= -p " << pid << endl;
53 
54  if (shell.get(value))
55  return value;
56  else
57  THROW(JSystemException, "No process data for PID " << pid);
58  }
59 
60 
61  /**
62  * Get memory usage in percent of this process.
63  *
64  * \param shell shell interface
65  * \return memory usage [%]
66  */
67  inline float getMemoryUsage(JShell& shell)
68  {
69  return getMemoryUsage(shell, getpid());
70  }
71 
72 
73  /**
74  * Get memory usage in percent of this process.
75  *
76  * \return memory usage [%]
77  */
78  inline float getMemoryUsage()
79  {
80  return getMemoryUsage(JShell::getInstance(), getpid());
81  }
82 
83 
84  /**
85  * Get cpu usage in percent of given process identifier.
86  *
87  * \param shell shell interface
88  * \param pid process identifier
89  * \return cpu usage [%]
90  */
91  inline float getCpuUsage(JShell& shell, const pid_t pid)
92  {
93  using namespace std;
94 
95  float value = 0.0;
96 
97  shell << "ps -o %cpu= -p " << pid << endl;
98 
99  if (shell.get(value))
100  return value;
101  else
102  THROW(JSystemException, "No process data for PID " << pid);
103  }
104 
105 
106  /**
107  * Get cpu usage in percent of this process.
108  *
109  * \param shell shell interface
110  * \return cpu usage [%]
111  */
112  inline float getCpuUsage(JShell& shell)
113  {
114  return getCpuUsage(shell, getpid());
115  }
116 
117 
118  /**
119  * Get cpu usage in percent of this process.
120  *
121  * \return cpu usage [%]
122  */
123  inline float getCpuUsage()
124  {
125  return getCpuUsage(JShell::getInstance(), getpid());
126  }
127 
128 
129  /**
130  * Get process identifier.
131  *
132  * \param shell shell interface
133  * \param process process name
134  * \return process identifier
135  */
136  inline pid_t getPID(JShell& shell, const char* process)
137  {
138  using namespace std;
139 
140  pid_t pid = -1;
141 
142  shell << "ps -o pid= -C " << process << endl;
143 
144  if (shell.get(pid))
145  return pid;
146  else
147  THROW(JSystemException, "No process " << process);
148  }
149 
150 
151  /**
152  * Get process identifier.
153  *
154  * \param process process name
155  * \return process identifier
156  */
157  inline pid_t getPID(const char* process)
158  {
159  return getPID(JShell::getInstance(), process);
160  }
161 
162 
163  /**
164  * Get parent process identifier.
165  *
166  * \param shell shell interface
167  * \param pid process identifier
168  * \return parent identifier
169  */
170  inline pid_t getParentID(JShell& shell, pid_t pid)
171  {
172  using namespace std;
173 
174  shell << "ps -o ppid= -p " << pid << endl;
175 
176  if (shell.get(pid))
177  return pid;
178  else
179  THROW(JSystemException, "No parent identifier " << pid);
180  }
181 
182 
183  /**
184  * Get parent process identifier.
185  *
186  * \param pid process identifier
187  * \return parent identifier
188  */
189  inline pid_t getParentID(const pid_t pid)
190  {
191  return getParentID(JShell::getInstance(), getpid());
192  }
193 
194 
195  /**
196  * Get shell name.
197  *
198  * \param shell shell interface
199  * \return shell name
200  */
201  inline std::string getShell(JShell& shell)
202  {
203  using namespace std;
204 
205  static string value = "";
206 
207  if (value == "") {
208 
209  pid_t pid = getppid();
210 
211  shell << "ps -o ppid= -o args= -p " << pid << endl;
212 
213  if (shell >> pid >> value) {
214 
215  shell.flush();
216 
217  if (!value.empty() && value[0] == '-') {
218  value = value.substr(1);
219  }
220 
221  } else {
222 
223  static_cast<istream&>(shell).clear();
224  shell.flush();
225 
226  THROW(JSystemException, "No shell");
227  }
228  }
229 
230  return value;
231  }
232 
233 
234  /**
235  * Get shell name of this process.
236  *
237  * \return shell name
238  */
239  inline std::string getShell()
240  {
241  return getShell(JShell::getInstance());
242  }
243 
244 
245  /**
246  * Get RAM of this CPU.
247  *
248  * \return number of bytes
249  */
250  inline unsigned long long int getRAM()
251  {
252  const JSysinfo info;
253 
254  return info.getTotalRAM();
255  }
256 
257 
258  /**
259  * Get process path.
260  *
261  * \param shell shell interface
262  * \param process process name
263  * \return path
264  */
265  inline const std::string which(JShell& shell, const char* process)
266  {
267  using namespace std;
268 
269  string buffer;
270 
271  shell << "which " << process << endl;
272 
273  shell.getline(buffer);
274 
275  shell.flush();
276 
277  return buffer;
278  }
279 
280 
281  /**
282  * Get process path.
283  *
284  * \param process process name
285  * \return path
286  */
287  inline const std::string which(const char* process)
288  {
289  return which(JShell::getInstance(), process);
290  }
291 
292 
293  /**
294  * Auxiliary class to list files.
295  */
296  struct ls :
297  std::vector<std::string>
298  {
299  /**
300  * Constructor.
301  *
302  * \param shell shell interface
303  * \param option option
304  */
305  ls(JShell& shell, const std::string& option = "")
306  {
307  get(shell, option);
308  }
309 
310 
311  /**
312  * Constructor.
313  *
314  * \param option option
315  */
316  ls(const std::string& option = "")
317  {
318  get(JShell::getInstance(), option);
319  }
320 
321 
322  /**
323  * add list files.
324  *
325  * \param shell shell interface
326  * \param option option
327  */
328  void get(JShell& shell, const std::string option = "")
329  {
330  using namespace std;
331 
332  string buffer = "ls";
333 
334  if (!option.empty()) {
335  buffer += " " + option;
336  }
337 
338  shell << buffer << endl;
339 
340  while (shell.getline(buffer)) {
341  push_back(buffer);
342  }
343  }
344  };
345 
346 
347  /**
348  * Print method.
349  *
350  * \param message message
351  */
352  inline void gprint(const std::string& message)
353  {
354  using namespace std;
355 
356  istringstream is(message);
357 
358  for (string buffer; getline(is, buffer); ) {
359  cout << "echo \"" << buffer << "\";" << endl;
360  }
361  }
362 
363 
364  /**
365  * Exit method.
366  *
367  * \param status exit status
368  * \param message optional message
369  */
370  inline int gexit(int status, const std::string& message = "")
371  {
372  using namespace std;
373 
374  gprint(message);
375 
376  cout << "exit " << status << endl;
377 
378  return status;
379  }
380 
381 
382  /**
383  * Set environment variable.
384  *
385  * This method prints the shell command to set the variable with the given name to the specified value.
386  *
387  * \param name variable name
388  * \param value variable value
389  */
390  inline void set_variable(const std::string& name, const std::string& value)
391  {
392  using namespace std;
393  using namespace JSYSTEM;
394 
395  static const string shell = getShell();
396 
397  string buffer(value);
398 
399  if (buffer.find(' ') != string::npos) {
400  buffer = "\"" + buffer + "\"";
401  }
402 
403  if (shell.find(ZSH) != string::npos ||
404  shell.find(KSH) != string::npos ||
405  shell.find(BASH) != string::npos)
406 
407  cout << "export " << name << "=" << buffer << ";" << endl;
408 
409  else if (shell.find(CSH) != string::npos ||
410  shell.find(TCSH) != string::npos)
411 
412  cout << "setenv " << name << " " << buffer << ";" << endl;
413 
414  else
415 
416  THROW(JSystemException, "unknown shell " << shell);
417  }
418 };
419 
420 #endif
Auxiliary class for file status.
Definition: JSysinfo.hh:21
The JShell clas can be used to interact with the shell via I/O streams.
Definition: JShell.hh:32
System information.
std::string getShell(JShell &shell)
Get shell name.
Exceptions.
float getMemoryUsage(JShell &shell, const pid_t pid)
Get memory usage in percent of given process identifier.
void set_variable(const std::string &name, const std::string &value)
Set environment variable.
static const char *const TCSH
void gprint(const std::string &message)
Print method.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
ls(const std::string &option="")
Constructor.
float getCpuUsage(JShell &shell, const pid_t pid)
Get cpu usage in percent of given process identifier.
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:468
pid_t getPID(JShell &shell, const char *process)
Get process identifier.
JShell & flush(std::ostream &out=null)
Extracts characters from this shell and flush them to the given output stream until the prompt is rea...
Definition: JShell.hh:184
static const char *const ZSH
Shell names.
bool get(T &value)
Get value.
Definition: JShell.hh:106
int gexit(int status, const std::string &message="")
Exit method.
const std::string which(JShell &shell, const char *process)
Get process path.
bool getline(std::string &buffer, const char eol= '\n')
Get line of text.
Definition: JShell.hh:137
ls(JShell &shell, const std::string &option="")
Constructor.
static JShell & getInstance()
Get reference to unique instance of this class object.
Definition: JShell.hh:76
unsigned long long int getRAM()
Get RAM of this CPU.
Shell interaction via I/O streams.
Exception for system call.
Definition: JException.hh:486
unsigned long long int getTotalRAM() const
Get total RAM.
Definition: JSysinfo.hh:38
Auxiliary class to list files.
static const char *const BASH
pid_t getParentID(JShell &shell, pid_t pid)
Get parent process identifier.
static const char *const KSH
static const char *const CSH