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
18from __future__ import (absolute_import, division, print_function)
19__metaclass__ = type
20
21import sys
22import glob
23
24from ansible.module_utils.facts.utils import get_file_lines
25from ansible.module_utils.facts.collector import BaseFactCollector
26
27
28class FcWwnInitiatorFactCollector(BaseFactCollector):
29    name = 'fibre_channel_wwn'
30    _fact_ids = set()
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            """
48            on solaris 10 or solaris 11 should use `fcinfo hba-port`
49            TBD (not implemented): on solaris 9 use `prtconf -pv`
50            """
51            cmd = module.get_bin_path('fcinfo')
52            cmd = cmd + " hba-port"
53            rc, fcinfo_out, err = module.run_command(cmd)
54            """
55            # fcinfo hba-port  | grep "Port WWN"
56            HBA Port WWN: 10000090fa1658de
57            """
58            if fcinfo_out:
59                for line in fcinfo_out.splitlines():
60                    if 'Port WWN' in line:
61                        data = line.split(' ')
62                        fc_facts['fibre_channel_wwn'].append(data[-1].rstrip())
63        elif sys.platform.startswith('aix'):
64            # get list of available fibre-channel devices (fcs)
65            cmd = module.get_bin_path('lsdev')
66            cmd = cmd + " -Cc adapter -l fcs*"
67            rc, lsdev_out, err = module.run_command(cmd)
68            if lsdev_out:
69                lscfg_cmd = module.get_bin_path('lscfg')
70                for line in lsdev_out.splitlines():
71                    # if device is available (not in defined state), get its WWN
72                    if 'Available' in line:
73                        data = line.split(' ')
74                        cmd = lscfg_cmd + " -vl %s" % data[0]
75                        rc, lscfg_out, err = module.run_command(cmd)
76                        # example output
77                        # lscfg -vpl fcs3 | grep "Network Address"
78                        #        Network Address.............10000090FA551509
79                        for line in lscfg_out.splitlines():
80                            if 'Network Address' in line:
81                                data = line.split('.')
82                                fc_facts['fibre_channel_wwn'].append(data[-1].rstrip())
83        return fc_facts
84