Skip to content

Commit

Permalink
Add tcp method for direct NIS access
Browse files Browse the repository at this point in the history
  • Loading branch information
nocarryr committed Aug 4, 2017
1 parent 43912a9 commit 2c26a23
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,8 @@ def apcaccess_gen(monkeypatch):
print('teardown ApcAccessGenerator')
g.stop()
print('teardown complete')

@pytest.fixture(params=[True, False])
def apcaccess_available(request, monkeypatch):
monkeypatch.setattr('upslogger.apcdata.APCACCESS_AVAILABLE', request.param)
return request.param
4 changes: 2 additions & 2 deletions tests/test_apcaccess.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import pytz

def test_apcaccessgen(tz_override, apcaccess_gen):
def test_apcaccessgen(tz_override, apcaccess_gen, apcaccess_available):
from upslogger import apcdata
assert apcdata.APC_HOSTPORT == apcaccess_gen.hostport
assert not apcdata.LOG_FILENAME.startswith('~')
Expand All @@ -13,7 +13,7 @@ def test_apcaccessgen(tz_override, apcaccess_gen):
continue
assert field.value == gen_data[field.name]

def test_linev(tz_override, apcaccess_gen):
def test_linev(tz_override, apcaccess_gen, apcaccess_available):
from upslogger import apcdata

apcaccess_gen.LINEV = 110.0
Expand Down
40 changes: 39 additions & 1 deletion upslogger/apcdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import io
import sys
import time
import socket
import errno
import datetime
import subprocess
import shlex
Expand All @@ -17,10 +19,11 @@

PY3 = sys.version_info.major >= 3

APCACCESS_AVAILABLE = True
APC_HOSTNAME = 'localhost'
APC_HOSTPORT = 3551

def get_apc_status(hostname=None, port=None):
def get_apc_status_subprocess(hostname=None, port=None):
if hostname is None:
hostname = APC_HOSTNAME
if port is None:
Expand All @@ -38,6 +41,41 @@ def get_apc_status(hostname=None, port=None):
d[key] = field
return d

def get_apc_status_tcp(hostname=None, port=None):
if hostname is None:
hostname = APC_HOSTNAME
if port is None:
port = APC_HOSTPORT
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((hostname, port))
sock.sendall(b'\x00\x06status')
resp = sock.recv(1024)
d = {}
for line in resp.split(b'\x00'):
line = line[1:].strip(b'\n')
if PY3:
line = line.decode('UTF-8')
if ':' in line:
key = line.split(':')[0].strip(' ')
val = ':'.join(line.split(':')[1:]).strip(' ')
field = Field.from_string(val, key)
d[key] = field
return d

def get_apc_status(hostname=None, port=None):
global APCACCESS_AVAILABLE
if not APCACCESS_AVAILABLE:
return get_apc_status_tcp(hostname, port)
try:
d = get_apc_status_subprocess(hostname, port)
except OSError as e:
if e.errno == errno.ENOENT:
APCACCESS_AVAILABLE = False
d = get_apc_status_tcp(hostname, port)
else: # pragma: no cover
raise
return d

def get_apc_linev(hostname=None, port=None):
d = get_apc_status(hostname, port)
r = {}
Expand Down

0 comments on commit 2c26a23

Please sign in to comment.