Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE3
/
monitor-client
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
1
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
2dd60e88
authored
Dec 14, 2025
by
Szeberényi Imre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
more exception safe
parent
b7d0105b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
39 deletions
+62
-39
miscellaneous/monitor-client.service
+1
-0
src/client.py
+61
-39
No files found.
miscellaneous/monitor-client.service
View file @
2dd60e88
...
@@ -5,6 +5,7 @@ After=network.target
...
@@ -5,6 +5,7 @@ After=network.target
[Service]
[Service]
User=cloud
User=cloud
Group=cloud
Group=cloud
SyslogIdentifier=monitor-client
WorkingDirectory=/home/cloud/monitor-client
WorkingDirectory=/home/cloud/monitor-client
ExecStart=/bin/bash -c "source /etc/profile; workon monitor-client; exec python manage.py run"
ExecStart=/bin/bash -c "source /etc/profile; workon monitor-client; exec python manage.py run"
Restart=always
Restart=always
...
...
src/client.py
View file @
2dd60e88
...
@@ -156,53 +156,69 @@ class Client:
...
@@ -156,53 +156,69 @@ class Client:
now
=
time
.
time
()
now
=
time
.
time
()
running_vms
=
[]
running_vms
=
[]
parser
=
argparse
.
ArgumentParser
(
add_help
=
False
)
parser
.
add_argument
(
'-name'
)
# '-m ' (with trailing space) is intentional to avoid collision with '-machine'
parser
.
add_argument
(
'--memory-size'
,
'-m '
,
dest
=
'memory_size'
,
type
=
parse_memory_size
)
try
:
try
:
for
entry
in
psutil
.
process_iter
():
for
entry
in
psutil
.
process_iter
(
attrs
=
[
'pid'
,
'name'
,
'cmdline'
]):
if
entry
.
name
()
in
(
'kvm'
,
'qemu-system-x86_64'
):
info
=
entry
.
info
parser
=
argparse
.
ArgumentParser
(
add_help
=
False
)
pid
=
info
.
get
(
'pid'
)
parser
.
add_argument
(
'-name'
)
name
=
info
.
get
(
'name'
)
# FONTOS: nincs szóköz a '-m' végén, és saját parse függvényt használunk
cmdline
=
info
.
get
(
'cmdline'
)
parser
.
add_argument
(
'--memory-size'
,
'-m '
,
dest
=
'memory_size'
,
if
not
name
or
name
not
in
(
'kvm'
,
'qemu-system-x86_64'
):
type
=
parse_memory_size
)
continue
args
,
unknown
=
parser
.
parse_known_args
(
entry
.
cmdline
()[
1
:])
if
not
cmdline
or
len
(
cmdline
)
<
2
:
logger
.
debug
(
'Pid
%
s: empty or too short cmdline:
%
r'
,
pid
,
cmdline
)
# for Red Hat style parametering of kvm
continue
if
isinstance
(
args
.
name
,
str
):
args
.
name
=
re
.
sub
(
r"^guest="
,
""
,
args
.
name
)
try
:
args
.
name
=
re
.
sub
(
r",debug-threads=.*$"
,
""
,
args
.
name
)
args
,
unknown
=
parser
.
parse_known_args
(
cmdline
[
1
:])
except
SystemExit
:
process
=
self
.
processes
.
get
(
entry
.
pid
,
None
)
logger
.
warning
(
"Argparse failed pid=
%
s cmdline=
%
r"
,
pid
,
cmdline
)
if
not
process
or
process
.
cmdline
()
!=
entry
.
cmdline
():
continue
process
=
psutil
.
Process
(
entry
.
pid
)
if
isinstance
(
args
.
name
,
str
):
logger
.
info
(
'New process:
%
s'
,
process
)
args
.
name
=
re
.
sub
(
r"^guest="
,
""
,
args
.
name
)
self
.
processes
[
entry
.
pid
]
=
process
args
.
name
=
re
.
sub
(
r",debug-threads=.*$"
,
""
,
args
.
name
)
if
args
.
memory_size
:
process
=
self
.
processes
.
get
(
pid
,
None
)
# args.memory_size MiB-ben van
if
not
process
:
rss_bytes
=
float
(
process
.
memory_info
()
.
rss
)
process
=
psutil
.
Process
(
pid
)
mem_bytes_total
=
args
.
memory_size
*
1024
**
2
logger
.
info
(
'New process:
%
s'
,
process
)
mem_perc
=
(
rss_bytes
/
mem_bytes_total
)
*
90.0
self
.
processes
[
pid
]
=
process
# no cpu_perc info when process appears first
metrics
.
append
(
'vm.
%(name)
s.memory.usage
%(value)
f
%(time)
d'
%
{
else
:
'name'
:
args
.
name
,
'value'
:
mem_perc
,
'time'
:
now
,
})
else
:
logger
.
warning
(
'Pid
%
d: no memory size found for VM
%
s'
,
entry
.
pid
,
args
.
name
)
cpu_perc
=
process
.
cpu_percent
()
cpu_perc
=
process
.
cpu_percent
()
metrics
.
append
(
'vm.
%(name)
s.cpu.percent
%(value)
f
%(time)
d'
%
{
metrics
.
append
(
'vm.
%(name)
s.cpu.percent
%(value)
f
%(time)
d'
%
{
'name'
:
args
.
name
,
'name'
:
args
.
name
,
'value'
:
cpu_perc
,
'value'
:
cpu_perc
,
'time'
:
now
,
'time'
:
now
,
})
})
running_vms
.
append
(
args
.
name
)
if
args
.
memory_size
:
# args.memory_size MiB-ben van
rss_bytes
=
float
(
process
.
memory_info
()
.
rss
)
mem_bytes_total
=
args
.
memory_size
*
1024
**
2
mem_perc
=
(
rss_bytes
/
mem_bytes_total
)
*
90.0
metrics
.
append
(
'vm.
%(name)
s.memory.usage
%(value)
f
%(time)
d'
%
{
'name'
:
args
.
name
,
'value'
:
mem_perc
,
'time'
:
now
,
})
else
:
else
:
logger
.
warning
(
'Pid:
%
d args.name is empty?:
%
s
%
s'
,
logger
.
warning
(
'Pid
%
d: no memory size found for VM
%
s'
,
pid
,
args
.
name
)
entry
.
pid
,
entry
.
name
(),
entry
.
cmdline
())
except
(
psutil
.
NoSuchProcess
,
TypeError
):
running_vms
.
append
(
args
.
name
)
logger
.
warning
(
'Process
%
d lost. Entry:
%
s'
,
entry
.
pid
,
entry
.
cmdline
()[
0
:])
else
:
logger
.
warning
(
'Pid:
%
d args.name is empty?:
%
s
%
s'
,
pid
,
name
,
cmdline
)
except
Exception
:
logger
.
exception
(
"collect_vms failed: pid=
%
s name=
%
r cmdline=
%
r"
,
locals
()
.
get
(
"pid"
,
"?"
),
locals
()
.
get
(
"name"
,
None
),
locals
()
.
get
(
"cmdline"
,
None
),
)
interfaces
=
psutil
.
net_io_counters
(
pernic
=
True
)
interfaces
=
psutil
.
net_io_counters
(
pernic
=
True
)
for
interface
,
data
in
interfaces
.
items
():
for
interface
,
data
in
interfaces
.
items
():
...
@@ -243,8 +259,10 @@ class Client:
...
@@ -243,8 +259,10 @@ class Client:
metricCollectors parameter that should be provided by the collectables
metricCollectors parameter that should be provided by the collectables
modul to work properly.
modul to work properly.
"""
"""
CACHE_TTL
=
1
*
24
*
3600
# One day
self
.
connect
()
self
.
connect
()
self
.
processes
=
{}
self
.
processes
=
{}
self
.
last_cache_cleaup
=
time
.
time
()
try
:
try
:
while
True
:
while
True
:
metrics
=
self
.
collect_node
()
+
self
.
collect_vms
()
metrics
=
self
.
collect_node
()
+
self
.
collect_vms
()
...
@@ -253,7 +271,11 @@ class Client:
...
@@ -253,7 +271,11 @@ class Client:
self
.
send
(
chunk
)
self
.
send
(
chunk
)
logger
.
debug
(
"metrics:
%
r"
,
metrics
)
logger
.
debug
(
"metrics:
%
r"
,
metrics
)
logger
.
info
(
"
%
d metrics sent"
,
len
(
metrics
))
logger
.
info
(
"
%
d metrics sent"
,
len
(
metrics
))
if
time
.
time
()
-
self
.
last_cache_cleaup
>
CACHE_TTL
:
self
.
processes
.
clear
()
self
.
last_cache_cleaup
=
time
.
time
()
time
.
sleep
(
10
)
time
.
sleep
(
10
)
except
KeyboardInterrupt
:
except
KeyboardInterrupt
:
logger
.
info
(
"Reporting has stopped by the user. Exiting..."
)
logger
.
info
(
"Reporting has stopped by the user. Exiting..."
)
finally
:
finally
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment