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