-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathprocessfdw.py
96 lines (74 loc) · 2.62 KB
/
processfdw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
Purpose
-------
This foreign data wrapper can be to list processes according to the
psutil module.
The column names are mapped to the :py:class:`psutil.Process` attributes.
.. api_compat: :read:
Usage Example
-------------
.. code-block:: sql
create foreign table processes (
pid int,
ppid int,
name text,
exe text,
cmdline text,
create_time timestamptz,
status text,
cwd text,
uids int[],
gids int[],
terminal text,
nice int,
ionice float[],
rlimit int[],
num_ctx_switches bigint[],
num_fds int,
num_threads int,
cpu_times interval[],
cpu_percent float,
cpu_affinity bigint[],
memory_info bigint[],
memory_percent float,
open_files text[],
connections text[],
is_running bool
) server process_server
.. code-block:: bash
ro= select name, cmdline, cpu_percent from processes where name = 'postgres';
name | cmdline | cpu_percent
----------+----------------------------------------------+-------------
postgres | ['/home/ro/pgdev/bin/postgres'] | 0
postgres | ['postgres: checkpointer process '] | 0
postgres | ['postgres: writer process '] | 0
postgres | ['postgres: wal writer process '] | 0
postgres | ['postgres: autovacuum launcher process '] | 0
postgres | ['postgres: stats collector process '] | 0
postgres | ['postgres: ro ro [local] SELECT'] | 9
(7 rows)
Options
-------
No options.
"""
from . import ForeignDataWrapper
from datetime import datetime
import psutil
DATE_COLUMNS = ['create_time']
class ProcessFdw(ForeignDataWrapper):
"""A foreign datawrapper for querying system stats.
It accepts no options.
You can define any column named after a statgrab column.
See the statgrab documentation.
"""
def _convert(self, key, value):
if key in DATE_COLUMNS:
if isinstance(value, (list, tuple)):
return [datetime.fromtimestamp(v) for v in value]
else:
return datetime.fromtimestamp(value)
return value
def execute(self, quals, columns):
for process in psutil.process_iter():
yield dict([(key, self._convert(key, value))
for key, value in process.as_dict(columns).items()])