Debian Codegraph @

lib/ansible/module_utils/facts/network/fc_wwn.py

raw
1 # Fibre Channel WWN initiator related facts collection for ansible.
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import annotations
19
20 import sys
21 import glob
22 import typing as t
23
24 from ansible.module_utils.facts.utils import get_file_lines
25 from ansible.module_utils.facts.collector import BaseFactCollector
26
27
28 class FcWwnInitiatorFactCollector(BaseFactCollector):
29 name = 'fibre_channel_wwn'
30 _fact_ids = set() # type: t.Set[str]
31
32 def collect(self, module=None, collected_facts=None):
33 """
34 Example contents /sys/class/fc_host/*/port_name:
35
36 0x21000014ff52a9bb
37
38 """
39
40 fc_facts = {}
41 fc_facts['fibre_channel_wwn'] = []
42 if sys.platform.startswith('linux'):
43 for fcfile in glob.glob('/sys/class/fc_host/*/port_name'):
44 for line in get_file_lines(fcfile):
45 fc_facts['fibre_channel_wwn'].append(line.rstrip()[2:])
46 elif sys.platform.startswith('sunos'):
47 # on solaris 10 or solaris 11 should use `fcinfo hba-port`
48 # TBD (not implemented): on solaris 9 use `prtconf -pv`
49 cmd = module.get_bin_path('fcinfo')
50 if cmd:
51 cmd = cmd + " hba-port"
52 rc, fcinfo_out, err = module.run_command(cmd)
53 # fcinfo hba-port | grep "Port WWN"
54 # HBA Port WWN: 10000090fa1658de
55 if rc == 0 and fcinfo_out:
56 for line in fcinfo_out.splitlines():
57 if 'Port WWN' in line:
58 data = line.split(' ')
59 fc_facts['fibre_channel_wwn'].append(data[-1].rstrip())
60 elif sys.platform.startswith('aix'):
61 cmd = module.get_bin_path('lsdev')
62 lscfg_cmd = module.get_bin_path('lscfg')
63 if cmd and lscfg_cmd:
64 # get list of available fibre-channel devices (fcs)
65 cmd = cmd + " -Cc adapter -l fcs*"
66 rc, lsdev_out, err = module.run_command(cmd)
67 if rc == 0 and lsdev_out:
68 for line in lsdev_out.splitlines():
69 # if device is available (not in defined state), get its WWN
70 if 'Available' in line:
71 data = line.split(' ')
72 cmd = lscfg_cmd + " -vl %s" % data[0]
73 rc, lscfg_out, err = module.run_command(cmd)
74 # example output
75 # lscfg -vpl fcs3 | grep "Network Address"
76 # Network Address.............10000090FA551509
77 if rc == 0 and lscfg_out:
78 for line in lscfg_out.splitlines():
79 if 'Network Address' in line:
80 data = line.split('.')
81 fc_facts['fibre_channel_wwn'].append(data[-1].rstrip())
82 elif sys.platform.startswith('hp-ux'):
83 cmd = module.get_bin_path('ioscan')
84 fcmsu_cmd = module.get_bin_path(
85 'fcmsutil',
86 opt_dirs=['/opt/fcms/bin'],
87 )
88 # go ahead if we have both commands available
89 if cmd and fcmsu_cmd:
90 # ioscan / get list of available fibre-channel devices (fcd)
91 cmd = cmd + " -fnC FC"
92 rc, ioscan_out, err = module.run_command(cmd)
93 if rc == 0 and ioscan_out:
94 for line in ioscan_out.splitlines():
95 line = line.strip()
96 if '/dev/fcd' in line:
97 dev = line.split(' ')
98 # get device information
99 cmd = fcmsu_cmd + " %s" % dev[0]
100 rc, fcmsutil_out, err = module.run_command(cmd)
101 # lookup the following line
102 # N_Port Port World Wide Name = 0x50060b00006975ec
103 if rc == 0 and fcmsutil_out:
104 for line in fcmsutil_out.splitlines():
105 if 'N_Port Port World Wide Name' in line:
106 data = line.split('=')
107 fc_facts['fibre_channel_wwn'].append(data[-1].strip())
108 return fc_facts