-
Notifications
You must be signed in to change notification settings - Fork 163
/
pcied
129 lines (98 loc) · 3.86 KB
/
pcied
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python2
"""
pcied
PCIe device monitoring daemon for SONiC
"""
try:
import os
import signal
import subprocess
import sys
import threading
import swsssdk
from sonic_py_common.daemon_base import DaemonBase
from sonic_py_common import device_info
except ImportError as e:
raise ImportError(str(e) + " - required module not found")
#
# Constants ====================================================================
#
SYSLOG_IDENTIFIER = "pcied"
PCIE_RESULT_REGEX = "PCIe Device Checking All Test"
PCIE_TABLE_NAME = "PCIE_STATUS"
PCIE_CONF_FILE = 'pcie.yaml'
PCIED_MAIN_THREAD_SLEEP_SECS = 60
REDIS_HOSTIP = "127.0.0.1"
#
# Daemon =======================================================================
#
class DaemonPcied(DaemonBase):
def __init__(self, log_identifier):
super(DaemonPcied, self).__init__(log_identifier)
(platform_path, _) = device_info.get_paths_to_platform_and_hwsku_dirs()
pciefilePath = os.path.join(platform_path, "plugins", PCIE_CONF_FILE)
sys.path.append(os.path.abspath(pciefilePath))
if not os.path.exists(pciefilePath):
self.log_error("Platform pcie configuration file doesn't exist! Exiting ...")
sys.exit("Platform PCIe Configuration file doesn't exist!")
self.timeout = PCIED_MAIN_THREAD_SLEEP_SECS
self.stop_event = threading.Event()
self.state_db = swsssdk.SonicV2Connector(host=REDIS_HOSTIP)
self.state_db.connect("STATE_DB")
def check_pcie_devices(self):
cmd = [ 'sudo', 'pcieutil', 'pcie-check' ]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
resultInfo, err = p.communicate()
pcie_db_state = self.read_state_db("PCIE_STATUS|", "PCIE_DEVICES")
for line in resultInfo.splitlines():
if PCIE_RESULT_REGEX in line:
if "PASSED" in line and "PASSED" not in pcie_db_state:
self.update_state_db("PCIE_STATUS|", "PCIE_DEVICES", "PASSED")
self.log_info("PCIe device status check : PASSED")
elif "FAILED" in line and "PASSED" in pcie_db_state:
self.update_state_db("PCIE_STATUS|", "PCIE_DEVICES", "FAILED")
self.log_info("PCIe device status check : FAILED")
def read_state_db(self, key1, key2):
return self.state_db.get('STATE_DB', key1, key2)
def update_state_db(self, key1, key2, value):
self.state_db.set('STATE_DB', key1, key2, value)
# Signal handler
def signal_handler(self, sig, frame):
if sig == signal.SIGHUP:
self.log_info("Caught SIGHUP - ignoring...")
elif sig == signal.SIGINT:
self.log_info("Caught SIGINT - exiting...")
self.stop_event.set()
elif sig == signal.SIGTERM:
self.log_info("Caught SIGTERM - exiting...")
self.stop_event.set()
else:
self.log_warning("Caught unhandled signal '" + sig + "'")
# Initialize daemon
def init(self):
self.log_info("Start daemon init...")
# Deinitialize daemon
def deinit(self):
self.log_info("Start daemon deinit...")
# Run daemon
def run(self):
self.log_info("Starting up...")
# Start daemon initialization sequence
self.init()
# Start main loop
self.log_info("Start daemon main loop")
while not self.stop_event.wait(self.timeout):
# Check the Pcie device status
self.check_pcie_devices()
self.log_info("Stop daemon main loop")
# Start daemon deinitialization sequence
self.deinit()
self.log_info("Shutting down...")
#
# Main =========================================================================
#
def main():
pcied = DaemonPcied(SYSLOG_IDENTIFIER)
pcied.run()
if __name__ == '__main__':
main()