def test_interface_stats(mock_requests_get):

    fauxapi = PfsenseFauxapi(host=None, apikey=None, apisecret=None)
    mock_requests_get.return_value.status_code = 200
    mock_requests_get.return_value.text = '{"callid": "5c8dc5d40c07a", "action": "interface_stats", "message": "ok", "data": {"stats": {}}}'

    response = fauxapi.interface_stats(None)
    assert response['action'] == 'interface_stats'
    assert response['message'] == 'ok'
    assert response['data'] is not None
Esempio n. 2
0
# # config_restore
# =============================================================================
# print(json.dumps(
#     FauxapiLib.config_restore('/cf/conf/backup/config-1530604754.xml'))
# )

# system_stats
# =============================================================================
print(json.dumps(
    FauxapiLib.system_stats())
)

# interface_stats - NB: the real interface name, not an interface alias such as "WAN" or "LAN"
# =============================================================================
print(json.dumps(
    FauxapiLib.interface_stats('em0'))
)

# gateway_status
# =============================================================================
print(json.dumps(
    FauxapiLib.gateway_status())
)

# send_event - filter reload
# =============================================================================
print(json.dumps(
    FauxapiLib.send_event('filter reload'))
)

# send_event - interface all reload
Esempio n. 3
0
class PfsenseCollector(BaseCollector):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._fapi = PfsenseFauxapi(
            self.config['api_host'],
            self.config['key'],
            self.config['secret'],
        )

    def get_data_for_sub(self) -> List[Gdata]:
        ret = []
        try:
            stats = self._get_stats()
        except Exception as e:
            logging.error(
                f'Failed to get stats from {self.config["api_host"]}: {e}')
            return ret

        ret.extend(self._get_int_gdata(stats))
        ret.append(self._get_sys_gdata(stats))

        return ret

    def _get_int_gdata(self, stats) -> List[Gdata]:
        """
        Turn the interface stats into Gdata objects
        """
        ret = []

        for iface, data in stats['interface'].items():
            dsnames = []
            values = []
            dstypes = []

            for k, v in data.items():
                dsnames.append(k)
                if k == 'mtu':
                    dstypes.append('gauge')
                else:
                    dstypes.append('counter')
                values.append(float(v))

            ret.append(
                Gdata(
                    plugin='pfsense',
                    dtype='interface',
                    dtype_instance=iface,
                    dsnames=dsnames,
                    dstypes=dstypes,
                    values=values,
                    interval=float(self.config['interval']),
                    host=self.config['gdata_host'],
                ))

        return ret

    def _get_sys_gdata(self, stats) -> Gdata:
        return Gdata(
            plugin='pfsense',
            dtype='system',
            dsnames=list(stats['system'].keys()),
            dstypes=['gauge'] * len(stats['system']),
            values=list(stats['system'].values()),
            interval=float(self.config['interval']),
            host=self.config['gdata_host'],
        )

    def _get_stats(self):
        """
        Get stats from the server
        """
        ret = {'interface': {}}
        # First, get the interface stats
        for iface in self.config['interfaces'].split():
            iface = iface.strip()
            stats = self._fapi.interface_stats(iface)
            if stats and stats['message'] == 'ok':
                ret['interface'][iface] = stats['data']['stats']
            else:
                logging.warning(f'Failed to get interface stats for {iface}')

        # Now get the system stats
        stats = self._fapi.system_stats()
        if stats and stats['message'] == 'ok':
            ret['system'] = {
                'load_avg_1': float(stats['data']['stats']['load_average'][0]),
                'load_avg_5': float(stats['data']['stats']['load_average'][1]),
                'load_avg_15':
                float(stats['data']['stats']['load_average'][2]),
                'pfstateperc': float(stats['data']['stats']['pfstatepercent']),
                'mbufperc': float(stats['data']['stats']['mbufpercent']),
                'temp': float(stats['data']['stats']['temp']),
            }

        return ret
Esempio n. 4
0
# =============================================================================
print(json.dumps(FauxapiLib.config_backup_list()))

# # config_restore
# =============================================================================
# print(json.dumps(
#     FauxapiLib.config_restore('/cf/conf/backup/config-1530604754.xml'))
# )

# system_stats
# =============================================================================
print(json.dumps(FauxapiLib.system_stats()))

# interface_stats - NB: the real interface name, not an interface alias such as "WAN" or "LAN"
# =============================================================================
print(json.dumps(FauxapiLib.interface_stats('em0')))

# gateway_status
# =============================================================================
print(json.dumps(FauxapiLib.gateway_status()))

# send_event - filter reload
# =============================================================================
print(json.dumps(FauxapiLib.send_event('filter reload')))

# send_event - interface all reload
# =============================================================================
print(json.dumps(FauxapiLib.send_event('interface all reload')))

# rule_get - get all rules
# =============================================================================