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