fix netdev regex
This commit is contained in:
parent
5da6533106
commit
a00355ad4c
15
src/pvecommon/__init__.py
Normal file
15
src/pvecommon/__init__.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import pexpect
|
||||||
|
|
||||||
|
global_qm_timeout = 10
|
||||||
|
|
||||||
|
def qm_term_cmd(vm_id, cmd, timeout=global_qm_timeout):
|
||||||
|
child = pexpect.spawn(f'qm monitor {vm_id}')
|
||||||
|
try:
|
||||||
|
child.expect('qm>', timeout=timeout)
|
||||||
|
child.sendline(cmd)
|
||||||
|
child.expect('qm>', timeout=timeout)
|
||||||
|
raw_output = child.before.decode('utf-8').strip()
|
||||||
|
finally:
|
||||||
|
child.close()
|
||||||
|
|
||||||
|
return raw_output
|
||||||
@ -14,6 +14,7 @@ import cProfile
|
|||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
|
||||||
|
import pvecommon
|
||||||
import qmblock
|
import qmblock
|
||||||
|
|
||||||
DEFAULT_PORT = 9116
|
DEFAULT_PORT = 9116
|
||||||
@ -77,25 +78,13 @@ def get_memory_info(pid):
|
|||||||
|
|
||||||
|
|
||||||
def extract_nic_info_from_monitor(vm_id):
|
def extract_nic_info_from_monitor(vm_id):
|
||||||
child = pexpect.spawn(f'qm monitor {vm_id}')
|
raw_output = pvecommon.qm_term_cmd(vm_id, 'info network')
|
||||||
|
|
||||||
# Wait for the QEMU monitor prompt
|
|
||||||
child.expect('qm>', timeout=10)
|
|
||||||
|
|
||||||
# Execute 'info network'
|
|
||||||
child.sendline('info network')
|
|
||||||
|
|
||||||
# Wait for the prompt again
|
|
||||||
child.expect('qm>', timeout=10)
|
|
||||||
|
|
||||||
# Parse the output
|
|
||||||
raw_output = child.before.decode('utf-8').strip()
|
|
||||||
child.close()
|
|
||||||
nic_info_list = re.findall(r'(net\d+:.*?)(?=(net\d+:|$))', raw_output, re.S)
|
nic_info_list = re.findall(r'(net\d+:.*?)(?=(net\d+:|$))', raw_output, re.S)
|
||||||
|
|
||||||
nics_map = {}
|
nics_map = {}
|
||||||
|
|
||||||
for netdev, cfg in [x.strip().split(": ") for x in re.findall(r'[^\n]*(net\d+:[^\n]*)\n', raw_output, re.S)]:
|
for netdev, cfg in [x.strip().split(": ") for x in re.findall(r'(net\d+:.*?)(?:\r{0,2}\n|(?=\s*\\ net\d+:)|$)', raw_output, re.S)]:
|
||||||
for cfg_pair in cfg.split(","):
|
for cfg_pair in cfg.split(","):
|
||||||
if cfg_pair=='':
|
if cfg_pair=='':
|
||||||
continue
|
continue
|
||||||
@ -209,6 +198,7 @@ def main():
|
|||||||
parser.add_argument('--metrics-prefix', type=str, default=DEFAULT_PREFIX, help='<prefix>_ will be prepended to each metric name')
|
parser.add_argument('--metrics-prefix', type=str, default=DEFAULT_PREFIX, help='<prefix>_ will be prepended to each metric name')
|
||||||
parser.add_argument('--loglevel', type=str, default='INFO', help='Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)')
|
parser.add_argument('--loglevel', type=str, default='INFO', help='Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)')
|
||||||
parser.add_argument('--profile', type=str, default='false', help='collect metrics once, and print profiling stats')
|
parser.add_argument('--profile', type=str, default='false', help='collect metrics once, and print profiling stats')
|
||||||
|
parser.add_argument('--qm-terminal-timeout', type=int, default=10, help='timeout for qm terminal commands')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
@ -219,6 +209,7 @@ def main():
|
|||||||
|
|
||||||
global prefix
|
global prefix
|
||||||
prefix = args.metrics_prefix
|
prefix = args.metrics_prefix
|
||||||
|
pvecommon.global_qm_timeout = args.qm_terminal_timeout
|
||||||
|
|
||||||
for name, description, labels in gauge_settings:
|
for name, description, labels in gauge_settings:
|
||||||
gauge_dict[name] = Gauge(f"{prefix}_{name}", description, labels)
|
gauge_dict[name] = Gauge(f"{prefix}_{name}", description, labels)
|
||||||
|
|||||||
@ -2,6 +2,8 @@ import pexpect
|
|||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
import pvecommon
|
||||||
|
|
||||||
def get_device(disk_path):
|
def get_device(disk_path):
|
||||||
try:
|
try:
|
||||||
return os.readlink(disk_path).split('/')[-1]
|
return os.readlink(disk_path).split('/')[-1]
|
||||||
@ -9,16 +11,7 @@ def get_device(disk_path):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def extract_disk_info_from_monitor(vm_id):
|
def extract_disk_info_from_monitor(vm_id):
|
||||||
child = pexpect.spawn(f'qm monitor {vm_id}')
|
raw_output = pvecommon.qm_term_cmd(vm_id, 'info block')
|
||||||
# Wait for the QEMU monitor prompt
|
|
||||||
child.expect('qm>', timeout=10)
|
|
||||||
# Execute 'info block'
|
|
||||||
child.sendline('info block')
|
|
||||||
# Wait for the prompt again
|
|
||||||
child.expect('qm>', timeout=10)
|
|
||||||
# Parse the output
|
|
||||||
raw_output = child.before.decode('utf-8').strip()
|
|
||||||
child.close()
|
|
||||||
disks_map = {}
|
disks_map = {}
|
||||||
disks = [x.strip() for x in raw_output.split("drive-")[1:]]
|
disks = [x.strip() for x in raw_output.split("drive-")[1:]]
|
||||||
for disk in disks:
|
for disk in disks:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user