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