signature
stringlengths
8
3.44k
body
stringlengths
0
1.41M
docstring
stringlengths
1
122k
id
stringlengths
5
17
def post_item(self, id, params):
return self.post_data("<STR_LIT>" + str(id), params)<EOL>
Change an item title/body/date
f2744:c0:m7
def upload_to_experiment(self, id, params):
return self.post_file("<STR_LIT>" + str(id), params)<EOL>
Upload a file to an experiment
f2744:c0:m8
def upload_to_item(self, id, params):
return self.post_file("<STR_LIT>" + str(id), params)<EOL>
Upload a file to an item
f2744:c0:m9
def add_tag_to_experiment(self, id, params):
return self.post_data("<STR_LIT>" + str(id), params)<EOL>
Add a tag to an experiment
f2744:c0:m10
def add_tag_to_item(self, id, params):
return self.post_data("<STR_LIT>" + str(id), params)<EOL>
Add a tag to an item
f2744:c0:m11
def __perform_request(self, url, type=GET, params=None):
if params is None:<EOL><INDENT>params = {}<EOL><DEDENT>if not self.token:<EOL><INDENT>raise SetupError("<STR_LIT>")<EOL><DEDENT>if not self.endpoint:<EOL><INDENT>raise SetupError("<STR_LIT>")<EOL><DEDENT>if self.dev:<EOL><INDENT>requests.packages.urllib3.disable_warnings()<EOL><DEDENT>url = urljoin(self.endpoint, url)<...
This method will perform the real request, in this way we can customize only the "output" of the API call by using self.__call_api method. This method will return the request object.
f2745:c5:m1
def get_data(self, url, type=GET, params=None):
if params is None:<EOL><INDENT>params = dict()<EOL><DEDENT>req = self.__perform_request(url, type, params)<EOL>if req.status_code == <NUM_LIT>:<EOL><INDENT>return True<EOL><DEDENT>if req.status_code == <NUM_LIT>:<EOL><INDENT>raise NotFoundError()<EOL><DEDENT>try:<EOL><INDENT>data = req.json()<EOL><DEDENT>except ValueEr...
This method is a basic implementation of __call_api that checks errors too. In cas of success the method will return True or the content of the response to the request.
f2745:c5:m2
def post_data(self, url, params):
url = urljoin(self.endpoint, url)<EOL>headers = {'<STR_LIT>': self.token}<EOL>req = requests.post(url, headers=headers, data=params, verify=False)<EOL>if req.status_code == <NUM_LIT>:<EOL><INDENT>return True<EOL><DEDENT>if req.status_code == <NUM_LIT>:<EOL><INDENT>raise NotFoundError()<EOL><DEDENT>try:<EOL><INDENT>data...
POST some stuff to change title/date/body or create experiment
f2745:c5:m3
def post_file(self, url, params):
url = urljoin(self.endpoint, url)<EOL>headers = {'<STR_LIT>': self.token}<EOL>req = requests.post(url, headers=headers, files=params, verify=False)<EOL>if req.status_code == <NUM_LIT>:<EOL><INDENT>return True<EOL><DEDENT>if req.status_code == <NUM_LIT>:<EOL><INDENT>raise NotFoundError()<EOL><DEDENT>try:<EOL><INDENT>dat...
POST files
f2745:c5:m4
def get_version():
return '<STR_LIT:.>'.join(str(i) for i in VERSION[:<NUM_LIT:3>])<EOL>
Returns only digit parts of version.
f2746:m1
def render_to_json_response(self, context, **response_kwargs):
return JsonResponse(<EOL>self.get_data(context),<EOL>**response_kwargs<EOL>)<EOL>
Returns a JSON response, transforming 'context' to make the payload.
f2748:c2:m0
def get_data(self, context):
<EOL>return context<EOL>
Returns an object that will be serialized as JSON by json.dumps().
f2748:c2:m1
def anonymous_required(function):
def wrapper(*args, **kwargs):<EOL><INDENT>if args[<NUM_LIT:0>].user.is_authenticated():<EOL><INDENT>url = settings.ANONYMOUS_REQUIRED_REDIRECT_URL<EOL>return HttpResponseRedirect(reverse(url))<EOL><DEDENT>return function(*args, **kwargs)<EOL><DEDENT>return wrapper<EOL>
Redirect to user profile if user is already logged-in
f2750:m0
def startup_proc():
global NL_BASE<EOL>global NL_PROC<EOL>log.debug('<STR_LIT>')<EOL>NL_BASE = NapalmLogs(disable_security=True,<EOL>address=NAPALM_LOGS_TEST_ADDR,<EOL>port=NAPALM_LOGS_TEST_PORT,<EOL>publisher=[{'<STR_LIT>': {'<STR_LIT>': True}}],<EOL>listener=[{'<STR_LIT>': {}}],<EOL>publish_address=NAPALM_LOGS_TEST_PUB_ADDR,<EOL>publish...
Startup the napalm-logs process.
f2754:m0
def startup_local_client():
time.sleep(<NUM_LIT:2>)<EOL>global TEST_CLIENT<EOL>context = zmq.Context()<EOL>TEST_CLIENT = context.socket(zmq.SUB)<EOL>TEST_CLIENT.connect('<STR_LIT>'.format(<EOL>addr=NAPALM_LOGS_TEST_PUB_ADDR,<EOL>port=NAPALM_LOGS_TEST_PUB_PORT)<EOL>)<EOL>TEST_CLIENT.setsockopt(zmq.SUBSCRIBE, b'<STR_LIT>')<EOL>
Startup a local ZMQ client to receive the published messages.
f2754:m1
def expr_match(line, expr):
if line == expr:<EOL><INDENT>return True<EOL><DEDENT>if fnmatch.fnmatch(line, expr):<EOL><INDENT>return True<EOL><DEDENT>try:<EOL><INDENT>if re.match(r'<STR_LIT>'.format(expr), line):<EOL><INDENT>return True<EOL><DEDENT><DEDENT>except re.error:<EOL><INDENT>pass<EOL><DEDENT>return False<EOL>
Evaluate a line of text against an expression. First try a full-string match, next try globbing, and then try to match assuming expr is a regular expression. Originally designed to match minion IDs for whitelists/blacklists.
f2761:m0
def check_whitelist_blacklist(value, whitelist=None, blacklist=None):
if blacklist is not None:<EOL><INDENT>if not hasattr(blacklist, '<STR_LIT>'):<EOL><INDENT>blacklist = [blacklist]<EOL><DEDENT>try:<EOL><INDENT>for expr in blacklist:<EOL><INDENT>if expr_match(value, expr):<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>except TypeError:<EOL><INDENT>log.error('<STR_LIT>'.format(bl...
Check a whitelist and/or blacklist to see if the value matches it. value The item to check the whitelist and/or blacklist against. whitelist The list of items that are white-listed. If ``value`` is found in the whitelist, then the function returns ``True``. Otherwise, it returns ``False``. blacklist ...
f2761:m1
def _setup_ipc(self):
log.debug('<STR_LIT>')<EOL>self.ctx = zmq.Context()<EOL>self.sub = self.ctx.socket(zmq.SUB)<EOL>self.sub.bind(PUB_PX_IPC_URL)<EOL>self.sub.setsockopt(zmq.SUBSCRIBE, b'<STR_LIT>')<EOL>log.debug('<STR_LIT>', self.hwm)<EOL>try:<EOL><INDENT>self.sub.setsockopt(zmq.HWM, self.hwm)<EOL><DEDENT>except AttributeError:<EOL><INDE...
Setup the IPC PUB and SUB sockets for the proxy.
f2762:c0:m2
def start(self):
self._setup_ipc()<EOL>thread = threading.Thread(target=self._suicide_when_without_parent, args=(os.getppid(),))<EOL>thread.start()<EOL>signal.signal(signal.SIGTERM, self._exit_gracefully)<EOL>try:<EOL><INDENT>zmq.proxy(self.sub, self.pub)<EOL><DEDENT>except zmq.ZMQError as error:<EOL><INDENT>if self.__up is False:<EOL>...
Listen to messages and publish them.
f2762:c0:m3
def get_interface(name):
try:<EOL><INDENT>log.debug('<STR_LIT>', name)<EOL>return BUFFER_LOOKUP[name]<EOL><DEDENT>except KeyError:<EOL><INDENT>msg = '<STR_LIT>'.format(name)<EOL>log.error(msg, exc_info=True)<EOL>raise InvalidBufferException(msg)<EOL><DEDENT>
Return the serialize function.
f2764:m0
def _setup_ipc(self):
self.ctx = zmq.Context()<EOL>log.debug('<STR_LIT>', self._transport_type, self.pub_id)<EOL>self.sub = self.ctx.socket(zmq.SUB)<EOL>self.sub.connect(PUB_IPC_URL)<EOL>self.sub.setsockopt(zmq.SUBSCRIBE, b'<STR_LIT>')<EOL>try:<EOL><INDENT>self.sub.setsockopt(zmq.HWM, self.opts['<STR_LIT>'])<EOL><DEDENT>except AttributeErro...
Subscribe to the pub IPC and publish the messages on the right transport.
f2766:c0:m2
def _setup_transport(self):
if '<STR_LIT>' in self.error_whitelist:<EOL><INDENT>log.info('<STR_LIT>', self._transport_type, self.pub_id)<EOL><DEDENT>if '<STR_LIT>' in self.error_whitelist:<EOL><INDENT>log.info('<STR_LIT>', self._transport_type, self.pub_id)<EOL><DEDENT>transport_class = get_transport(self._transport_type)<EOL>log.debug('<STR_LIT>...
Setup the transport.
f2766:c0:m3
def _prepare(self, serialized_obj):
<EOL>nonce = nacl.utils.random(nacl.secret.SecretBox.NONCE_SIZE)<EOL>encrypted = self.__safe.encrypt(serialized_obj, nonce)<EOL>signed = self.__signing_key.sign(encrypted)<EOL>return signed<EOL>
Prepare the object to be sent over the untrusted channel.
f2766:c0:m4
def start(self):
<EOL>napalm_logs_publisher_received_messages = Counter(<EOL>'<STR_LIT>',<EOL>"<STR_LIT>",<EOL>['<STR_LIT>', '<STR_LIT:address>', '<STR_LIT:port>']<EOL>)<EOL>napalm_logs_publisher_whitelist_blacklist_check_fail = Counter(<EOL>'<STR_LIT>',<EOL>"<STR_LIT>",<EOL>['<STR_LIT>', '<STR_LIT:address>', '<STR_LIT:port>']<EOL>)<EO...
Listen to messages and publish them.
f2766:c0:m6
def __init__(self,<EOL>address='<STR_LIT>',<EOL>port=<NUM_LIT>,<EOL>listener='<STR_LIT>',<EOL>publisher='<STR_LIT>',<EOL>publish_address='<STR_LIT>',<EOL>publish_port=<NUM_LIT>,<EOL>auth_address='<STR_LIT>',<EOL>auth_port=<NUM_LIT>,<EOL>metrics_enabled=False,<EOL>metrics_address='<STR_LIT>',<EOL>metrics_port='<STR_LIT>...
self.address = address<EOL>self.port = port<EOL>self.listener = listener<EOL>self.publisher = publisher<EOL>self.publish_address = publish_address<EOL>self.publish_port = publish_port<EOL>self.auth_address = auth_address<EOL>self.auth_port = auth_port<EOL>self.metrics_enabled = metrics_enabled<EOL>self.metrics_address ...
Init the napalm-logs engine. :param address: The address to bind the syslog client. Default: 0.0.0.0. :param port: Listen port. Default: 514. :param listener: Listen type. Default: udp. :param publish_address: The address to bing when publishing the OC objects. Default: 0.0.0.0. :param publish...
f2767:c0:m0
def _setup_buffer(self):
if not self._buffer_cfg or not isinstance(self._buffer_cfg, dict):<EOL><INDENT>return<EOL><DEDENT>buffer_name = list(self._buffer_cfg.keys())[<NUM_LIT:0>]<EOL>buffer_class = napalm_logs.buffer.get_interface(buffer_name)<EOL>log.debug('<STR_LIT>', buffer_name)<EOL>if '<STR_LIT>' not in self._buffer_cfg[buffer_name]:<EOL...
Setup the buffer subsystem.
f2767:c0:m3
def _setup_metrics(self):
path = os.environ.get("<STR_LIT>")<EOL>if not os.path.exists(self.metrics_dir):<EOL><INDENT>try:<EOL><INDENT>log.info("<STR_LIT>")<EOL>os.makedirs(self.metrics_dir)<EOL><DEDENT>except OSError:<EOL><INDENT>log.error("<STR_LIT>")<EOL>raise ConfigurationException("<STR_LIT>")<EOL><DEDENT>path = self.metrics_dir<EOL><DEDEN...
Start metric exposition
f2767:c0:m4
def _setup_log(self):
logging_level = CONFIG.LOGGING_LEVEL.get(self.log_level.lower())<EOL>logging.basicConfig(format=self.log_format,<EOL>level=logging_level)<EOL>
Setup the log object.
f2767:c0:m5
def _post_preparation(self):
self.opts['<STR_LIT>'] = CONFIG.ZMQ_INTERNAL_HWM if self.hwm is None else self.hwm<EOL>self.opts['<STR_LIT>'] = False<EOL>for pub in self.publisher:<EOL><INDENT>pub_name = list(pub.keys())[<NUM_LIT:0>]<EOL>pub_opts = list(pub.values())[<NUM_LIT:0>]<EOL>error_whitelist = pub_opts.get('<STR_LIT>', [])<EOL>error_blacklist...
The steps for post-preparation (when the logs, and everything is already setup).
f2767:c0:m6
def _whitelist_blacklist(self, os_name):
return napalm_logs.ext.check_whitelist_blacklist(os_name,<EOL>whitelist=self.device_whitelist,<EOL>blacklist=self.device_blacklist)<EOL>
Determines if the OS should be ignored, depending on the whitelist-blacklist logic configured by the user.
f2767:c0:m7
@staticmethod<EOL><INDENT>def _extract_yaml_docstring(stream):<DEDENT>
comment_lines = []<EOL>lines = stream.read().splitlines()<EOL>for line in lines:<EOL><INDENT>line_strip = line.strip()<EOL>if not line_strip:<EOL><INDENT>continue<EOL><DEDENT>if line_strip.startswith('<STR_LIT:#>'):<EOL><INDENT>comment_lines.append(<EOL>line_strip.replace('<STR_LIT:#>', '<STR_LIT>', <NUM_LIT:1>).strip(...
Extract the comments at the top of the YAML file, from the stream handler. Return the extracted comment as string.
f2767:c0:m8
def _load_config(self, path):
config = {}<EOL>log.debug('<STR_LIT>', path)<EOL>if not os.path.isdir(path):<EOL><INDENT>msg = (<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>).format(path=path)<EOL>log.error(msg)<EOL>raise IOError(msg)<EOL><DEDENT>os_subdirs = [sdpath[<NUM_LIT:0>] for sdpath in os.walk(path)][<NUM_LIT:1>:]<EOL>if not os_subdirs:<EOL><INDENT>l...
Read the configuration under a specific path and return the object.
f2767:c0:m9
def _verify_config_dict(self, valid, config, dev_os, key_path=None):
if not key_path:<EOL><INDENT>key_path = []<EOL><DEDENT>for key, value in valid.items():<EOL><INDENT>self._verify_config_key(key, value, valid, config, dev_os, key_path)<EOL><DEDENT>
Verify if the config dict is valid.
f2767:c0:m13
def _verify_config(self):
if not self.config_dict:<EOL><INDENT>self._raise_config_exception('<STR_LIT>')<EOL><DEDENT>for dev_os, dev_config in self.config_dict.items():<EOL><INDENT>if not dev_config:<EOL><INDENT>log.warning('<STR_LIT>', dev_os)<EOL>continue<EOL><DEDENT>self._verify_config_dict(CONFIG.VALID_CONFIG, dev_config, dev_os)<EOL><DEDEN...
Verify that the config is correct
f2767:c0:m14
def _build_config(self):
if not self.config_dict:<EOL><INDENT>if not self.config_path:<EOL><INDENT>self.config_path = os.path.join(<EOL>os.path.dirname(os.path.realpath(__file__)),<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>log.info('<STR_LIT>', self.config_path)<EOL>self.config_dict = self._load_config(self.config_path)<EOL><DEDENT>if not self.extensi...
Build the config of the napalm syslog parser.
f2767:c0:m15
def _start_auth_proc(self):
log.debug('<STR_LIT>')<EOL>verify_key = self.__signing_key.verify_key<EOL>sgn_verify_hex = verify_key.encode(encoder=nacl.encoding.HexEncoder)<EOL>log.debug('<STR_LIT>')<EOL>auth = NapalmLogsAuthProc(self.certificate,<EOL>self.keyfile,<EOL>self.__priv_key,<EOL>sgn_verify_hex,<EOL>self.auth_address,<EOL>self.auth_port)<...
Start the authenticator process.
f2767:c0:m16
def _start_lst_proc(self,<EOL>listener_type,<EOL>listener_opts):
log.debug('<STR_LIT>', listener_type)<EOL>listener = NapalmLogsListenerProc(self.opts,<EOL>self.address,<EOL>self.port,<EOL>listener_type,<EOL>listener_opts=listener_opts)<EOL>proc = Process(target=listener.start)<EOL>proc.start()<EOL>proc.description = '<STR_LIT>'<EOL>log.debug('<STR_LIT>', proc._name, proc.pid)<EOL>r...
Start the listener process.
f2767:c0:m17
def _start_srv_proc(self,<EOL>started_os_proc):
log.debug('<STR_LIT>')<EOL>server = NapalmLogsServerProc(self.opts,<EOL>self.config_dict,<EOL>started_os_proc,<EOL>buffer=self._buffer)<EOL>proc = Process(target=server.start)<EOL>proc.start()<EOL>proc.description = '<STR_LIT>'<EOL>log.debug('<STR_LIT>', proc._name, proc.pid)<EOL>return proc<EOL>
Start the server process.
f2767:c0:m18
def _start_pub_proc(self,<EOL>publisher_type,<EOL>publisher_opts,<EOL>pub_id):
log.debug('<STR_LIT>', publisher_type)<EOL>publisher = NapalmLogsPublisherProc(self.opts,<EOL>self.publish_address,<EOL>self.publish_port,<EOL>publisher_type,<EOL>self.serializer,<EOL>self.__priv_key,<EOL>self.__signing_key,<EOL>publisher_opts,<EOL>disable_security=self.disable_security,<EOL>pub_id=pub_id)<EOL>proc = P...
Start the publisher process.
f2767:c0:m20
def _start_dev_proc(self,<EOL>device_os,<EOL>device_config):
log.info('<STR_LIT>', device_os)<EOL>dos = NapalmLogsDeviceProc(device_os,<EOL>self.opts,<EOL>device_config)<EOL>os_proc = Process(target=dos.start)<EOL>os_proc.start()<EOL>os_proc.description = '<STR_LIT>' % device_os<EOL>log.debug('<STR_LIT>', os_proc._name, device_os, os_proc.pid)<EOL>return os_proc<EOL>
Start the device worker process.
f2767:c0:m21
def start_engine(self):
if self.disable_security is True:<EOL><INDENT>log.warning('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>log.debug('<STR_LIT>')<EOL>self.__priv_key = nacl.utils.random(nacl.secret.SecretBox.KEY_SIZE)<EOL>log.debug('<STR_LIT>')<EOL>self.__signing_key = nacl.signing.SigningKey.generate()<EOL>self._processes.append(self._star...
Start the child processes (one per device OS)
f2767:c0:m22
def _check_children(self):
while self.up:<EOL><INDENT>time.sleep(<NUM_LIT:1>)<EOL>for process in self._processes:<EOL><INDENT>if process.is_alive() is True:<EOL><INDENT>continue<EOL><DEDENT>log.debug('<STR_LIT>', process.description)<EOL>self.stop_engine()<EOL><DEDENT><DEDENT>
Check all of the child processes are still running
f2767:c0:m23
def emit(msg_dict):
log.debug('<STR_LIT>')<EOL>log.debug(msg_dict)<EOL>ret = {}<EOL>extracted = napalm_logs.utils.extract(_RGX, msg_dict['<STR_LIT:message>'], _RGX_PARTS)<EOL>if not extracted:<EOL><INDENT>return ret<EOL><DEDENT>uid_key_path = '<STR_LIT>'.format(extracted)<EOL>uid_value = int(extracted['<STR_LIT>'])<EOL>log.debug('<STR_LIT...
Extracts the details from the syslog message and returns an object having the following structure: .. code-block:: python { u'users': { u'user': { u'luke': { u'action': { u'login': True }, u'uid...
f2768:m0
def _exit_gracefully(self, signum, _):
self.stop()<EOL>
Exit gracefully.
f2771:c0:m1
def _handshake(self, conn, addr):
<EOL>msg = conn.recv(len(MAGIC_REQ))<EOL>log.debug('<STR_LIT>', msg, addr)<EOL>if msg != MAGIC_REQ:<EOL><INDENT>log.warning('<STR_LIT>', msg, addr)<EOL>return<EOL><DEDENT>log.debug('<STR_LIT>')<EOL>conn.send(self.__key)<EOL>log.debug('<STR_LIT>')<EOL>msg = conn.recv(len(MAGIC_ACK))<EOL>if msg != MAGIC_ACK:<EOL><INDENT>...
Ensures that the client receives the AES key.
f2771:c0:m2
def keep_alive(self, conn):
while self.__up:<EOL><INDENT>msg = conn.recv(len(AUTH_KEEP_ALIVE))<EOL>if msg != AUTH_KEEP_ALIVE:<EOL><INDENT>log.error('<STR_LIT>', AUTH_KEEP_ALIVE)<EOL>conn.close()<EOL>return<EOL><DEDENT>try:<EOL><INDENT>conn.send(AUTH_KEEP_ALIVE_ACK)<EOL><DEDENT>except (IOError, socket.error) as err:<EOL><INDENT>log.error('<STR_LIT...
Maintains auth sessions
f2771:c0:m3
def verify_cert(self):
log.debug('<STR_LIT>',<EOL>self.certificate, self.keyfile)<EOL>try:<EOL><INDENT>ssl.create_default_context().load_cert_chain(self.certificate, keyfile=self.keyfile)<EOL><DEDENT>except ssl.SSLError:<EOL><INDENT>error_string = '<STR_LIT>'<EOL>log.error(error_string)<EOL>raise SSLMismatchException(error_string)<EOL><DEDEN...
Checks that the provided cert and key are valid and usable
f2771:c0:m4
def _create_skt(self):
log.debug('<STR_LIT>')<EOL>if '<STR_LIT::>' in self.auth_address:<EOL><INDENT>self.socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)<EOL><DEDENT>else:<EOL><INDENT>self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<EOL><DEDENT>try:<EOL><INDENT>self.socket.bind((self.auth_address, self.auth_port))<...
Create the authentication socket.
f2771:c0:m5
def start(self):
<EOL>log.debug('<STR_LIT>')<EOL>self.verify_cert()<EOL>self._create_skt()<EOL>log.debug('<STR_LIT>', AUTH_MAX_CONN)<EOL>self.socket.listen(AUTH_MAX_CONN)<EOL>thread = threading.Thread(target=self._suicide_when_without_parent, args=(os.getppid(),))<EOL>thread.start()<EOL>signal.signal(signal.SIGTERM, self._exit_graceful...
Listen to auth requests and send the AES key. Each client connection starts a new thread.
f2771:c0:m6
def stop(self):
log.info('<STR_LIT>')<EOL>self.__up = False<EOL>self.socket.close()<EOL>
Stop the auth proc.
f2771:c0:m7
def _setup_listener(self):
listener_class = get_listener(self._listener_type)<EOL>self.address = self.listener_opts.pop('<STR_LIT:address>', self.address)<EOL>self.port = self.listener_opts.pop('<STR_LIT:port>', self.port)<EOL>self.listener = listener_class(self.address,<EOL>self.port,<EOL>**self.listener_opts)<EOL>
Setup the transport.
f2772:c0:m2
def _setup_ipc(self):
log.debug('<STR_LIT>')<EOL>self.ctx = zmq.Context()<EOL>self.pub = self.ctx.socket(zmq.PUSH)<EOL>self.pub.connect(LST_IPC_URL)<EOL>log.debug('<STR_LIT>', self.opts['<STR_LIT>'])<EOL>try:<EOL><INDENT>self.pub.setsockopt(zmq.HWM, self.opts['<STR_LIT>'])<EOL><DEDENT>except AttributeError:<EOL><INDENT>self.pub.setsockopt(z...
Setup the listener ICP pusher.
f2772:c0:m3
def start(self):
<EOL>c_logs_ingested = Counter(<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>['<STR_LIT>', '<STR_LIT:address>', '<STR_LIT:port>'],<EOL>)<EOL>c_messages_published = Counter(<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>['<STR_LIT>', '<STR_LIT:address>', '<STR_LIT:port>'],<EOL>)<EOL>self._setup_ipc()<EOL>log.debug('<STR_LIT>', self._l...
Listen to messages and publish them.
f2772:c0:m4
def _setup_ipc(self):
log.debug('<STR_LIT>')<EOL>self.ctx = zmq.Context()<EOL>self.sub = self.ctx.socket(zmq.PULL)<EOL>self.sub.bind(LST_IPC_URL)<EOL>try:<EOL><INDENT>self.sub.setsockopt(zmq.HWM, self.opts['<STR_LIT>'])<EOL><DEDENT>except AttributeError:<EOL><INDENT>self.sub.setsockopt(zmq.RCVHWM, self.opts['<STR_LIT>'])<EOL><DEDENT>log.deb...
Setup the IPC pub and sub. Subscript to the listener IPC and publish to the device specific IPC.
f2773:c0:m2
def _cleanup_buffer(self):
if not self._buffer:<EOL><INDENT>return<EOL><DEDENT>while True:<EOL><INDENT>time.sleep(<NUM_LIT>)<EOL>log.debug('<STR_LIT>')<EOL>items = self._buffer.items()<EOL>log.debug('<STR_LIT>')<EOL>log.debug(list(items))<EOL><DEDENT>
Periodically cleanup the buffer.
f2773:c0:m3
def _compile_prefixes(self):
self.compiled_prefixes = {}<EOL>for dev_os, os_config in self.config.items():<EOL><INDENT>if not os_config:<EOL><INDENT>continue<EOL><DEDENT>self.compiled_prefixes[dev_os] = []<EOL>for prefix in os_config.get('<STR_LIT>', []):<EOL><INDENT>values = prefix.get('<STR_LIT>', {})<EOL>line = prefix.get('<STR_LIT>', '<STR_LIT...
Create a dict of all OS prefixes and their compiled regexs
f2773:c0:m4
def _identify_prefix(self, msg, data):
prefix_id = -<NUM_LIT:1><EOL>for prefix in data:<EOL><INDENT>msg_dict = {}<EOL>prefix_id += <NUM_LIT:1><EOL>match = None<EOL>if '<STR_LIT>' in prefix:<EOL><INDENT>log.debug('<STR_LIT>', prefix['<STR_LIT>'])<EOL>try:<EOL><INDENT>match = prefix['<STR_LIT>'](msg)<EOL><DEDENT>except Exception:<EOL><INDENT>log.error('<STR_L...
Check the message again each OS prefix and if matched return the message dict
f2773:c0:m5
def _identify_os(self, msg):
ret = []<EOL>for dev_os, data in self.compiled_prefixes.items():<EOL><INDENT>log.debug('<STR_LIT>', dev_os)<EOL>msg_dict = self._identify_prefix(msg, data)<EOL>if msg_dict:<EOL><INDENT>log.debug('<STR_LIT>', dev_os)<EOL>ret.append((dev_os, msg_dict))<EOL><DEDENT>else:<EOL><INDENT>log.debug('<STR_LIT>', dev_os)<EOL><DED...
Using the prefix of the syslog message, we are able to identify the operating system and then continue parsing.
f2773:c0:m6
def start(self):
<EOL>napalm_logs_server_messages_received = Counter(<EOL>"<STR_LIT>",<EOL>"<STR_LIT>"<EOL>)<EOL>napalm_logs_server_skipped_buffered_messages = Counter(<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>['<STR_LIT>']<EOL>)<EOL>napalm_logs_server_messages_with_identified_os = Counter(<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>['<STR_LIT...
Take the messages from the queue, inspect and identify the operating system, then queue the message correspondingly.
f2773:c0:m7
def bgp_state_convert(state):
state_dict = {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>'}<EOL>return state_dict.get(state, state.upper())<EOL>
Given a matched BGP state, map it to a vendor agnostic version.
f2774:m2
def bfd_state_convert(state):
state_dict = {'<STR_LIT>': '<STR_LIT>'}<EOL>return state_dict.get(state, state.upper())<EOL>
Given a matched BFD state, map it to a vendor agnostic version.
f2774:m3
def unserialize(binary):
return umsgpack.unpackb(binary)<EOL>
Unpack the original OpenConfig object, serialized using MessagePack. This is to be used when disable_security is set.
f2774:m4
def setval(key, val, dict_=None, delim=defaults.DEFAULT_DELIM):
if not dict_:<EOL><INDENT>dict_ = {}<EOL><DEDENT>prev_hier = dict_<EOL>dict_hier = key.split(delim)<EOL>for each in dict_hier[:-<NUM_LIT:1>]:<EOL><INDENT>if isinstance(each, six.string_type):<EOL><INDENT>if each not in prev_hier:<EOL><INDENT>prev_hier[each] = {}<EOL><DEDENT>prev_hier = prev_hier[each]<EOL><DEDENT>else:...
Set a value under the dictionary hierarchy identified under the key. The target 'foo/bar/baz' returns the dictionary hierarchy {'foo': {'bar': {'baz': {}}}}. .. note:: Currently this doesn't work with integers, i.e. cannot build lists dynamically. TODO
f2774:m6
def traverse(data, key, delim=defaults.DEFAULT_DELIM):
for each in key.split(delim):<EOL><INDENT>if isinstance(data, list):<EOL><INDENT>if isinstance(each, six.string_type):<EOL><INDENT>embed_match = False<EOL>for embedded in (x for x in data if isinstance(x, dict)):<EOL><INDENT>try:<EOL><INDENT>data = embedded[each]<EOL>embed_match = True<EOL>break<EOL><DEDENT>except KeyE...
Traverse a dict or list using a slash delimiter target string. The target 'foo/bar/0' will return data['foo']['bar'][0] if this value exists, otherwise will return empty dict. Return None when not found. This can be used to verify if a certain key exists under dictionary hierarchy.
f2774:m7
def dictupdate(dest, upd):
recursive_update = True<EOL>if (not isinstance(dest, collections.Mapping))or (not isinstance(upd, collections.Mapping)):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>updkeys = list(upd.keys())<EOL>if not set(list(dest.keys())) & set(updkeys):<EOL><INDENT>recursive_update = False<EOL><DEDENT>if recursive_update:...
Recursive version of the default dict.update Merges upd recursively into dest.
f2774:m8
def _start_keep_alive(self):
keep_alive_thread = threading.Thread(target=self.keep_alive)<EOL>keep_alive_thread.daemon = True<EOL>keep_alive_thread.start()<EOL>
Start the keep alive thread as a daemon
f2774:c0:m1
def keep_alive(self):
self.ssl_skt.settimeout(defaults.AUTH_KEEP_ALIVE_INTERVAL)<EOL>while self.__up:<EOL><INDENT>try:<EOL><INDENT>log.debug('<STR_LIT>')<EOL>self.ssl_skt.send(defaults.AUTH_KEEP_ALIVE)<EOL><DEDENT>except socket.error:<EOL><INDENT>log.error('<STR_LIT>')<EOL>log.error('<STR_LIT>')<EOL>self.reconnect()<EOL>log.debug('<STR_LIT>...
Send a keep alive request periodically to make sure that the server is still alive. If not then try to reconnect.
f2774:c0:m2
def reconnect(self):
log.debug('<STR_LIT>')<EOL>try:<EOL><INDENT>self.ssl_skt.close()<EOL><DEDENT>except socket.error:<EOL><INDENT>log.error('<STR_LIT>')<EOL><DEDENT>log.debug('<STR_LIT>')<EOL>self.authenticate()<EOL>
Try to reconnect and re-authenticate with the server.
f2774:c0:m3
def authenticate(self):
log.debug('<STR_LIT>',<EOL>self.address, self.port, self.certificate)<EOL>if '<STR_LIT::>' in self.address:<EOL><INDENT>skt_ver = socket.AF_INET6<EOL><DEDENT>else:<EOL><INDENT>skt_ver = socket.AF_INET<EOL><DEDENT>skt = socket.socket(skt_ver, socket.SOCK_STREAM)<EOL>self.ssl_skt = ssl.wrap_socket(skt,<EOL>ca_certs=self....
Authenticate the client and return the private and signature keys. Establish a connection through a secured socket, then do the handshake using the napalm-logs auth algorithm.
f2774:c0:m4
def decrypt(self, binary):
try:<EOL><INDENT>encrypted = self.verify_key.verify(binary)<EOL><DEDENT>except BadSignatureError:<EOL><INDENT>log.error('<STR_LIT>', exc_info=True)<EOL>raise BadSignatureException('<STR_LIT>')<EOL><DEDENT>try:<EOL><INDENT>packed = self.priv_key.decrypt(encrypted)<EOL><DEDENT>except CryptoError:<EOL><INDENT>log.error('<...
Decrypt and unpack the original OpenConfig object, serialized using MessagePack. Raise BadSignatureException when the signature was forged or corrupted.
f2774:c0:m5
def stop(self):
self.__up = False<EOL>self.ssl_skt.close()<EOL>
Stop the client.
f2774:c0:m6
def get_serializer(name):
try:<EOL><INDENT>log.debug('<STR_LIT>', name)<EOL>return SERIALIZER_LOOKUP[name]<EOL><DEDENT>except KeyError:<EOL><INDENT>msg = '<STR_LIT>'.format(name)<EOL>log.error(msg, exc_info=True)<EOL>raise InvalidSerializerException(msg)<EOL><DEDENT>
Return the serialize function.
f2776:m0
def start(self):
log.debug('<STR_LIT>',<EOL>self.bootstrap_servers,<EOL>self.group_id)<EOL>try:<EOL><INDENT>self.consumer = kafka.KafkaConsumer(bootstrap_servers=self.bootstrap_servers,<EOL>group_id=self.group_id)<EOL><DEDENT>except kafka.errors.NoBrokersAvailable as err:<EOL><INDENT>log.error(err, exc_info=True)<EOL>raise ListenerExce...
Startup the kafka consumer.
f2777:c0:m1
def receive(self):
try:<EOL><INDENT>msg = next(self.consumer)<EOL><DEDENT>except ValueError as error:<EOL><INDENT>log.error('<STR_LIT>', error, exc_info=True)<EOL>raise ListenerException(error)<EOL><DEDENT>log_source = msg.key<EOL>try:<EOL><INDENT>decoded = json.loads(msg.value.decode('<STR_LIT:utf-8>'))<EOL><DEDENT>except ValueError:<EO...
Return the message received and the address.
f2777:c0:m2
def stop(self):
log.info('<STR_LIT>')<EOL>self.consumer.unsubscribe()<EOL>self.consumer.close()<EOL>
Shutdown kafka consumer.
f2777:c0:m3
def start(self):
pass<EOL>
Starts the listener.
f2778:c0:m1
def receive(self):
pass<EOL>
Return an object read from the source, and the location identification object.
f2778:c0:m2
def stop(self):
pass<EOL>
Shuts down the listener.
f2778:c0:m3
def _client_connection(self, conn, addr):
log.debug('<STR_LIT>', addr[<NUM_LIT:0>], addr[<NUM_LIT:1>])<EOL>conn.settimeout(self.socket_timeout)<EOL>try:<EOL><INDENT>while self.__up:<EOL><INDENT>msg = conn.recv(self.buffer_size)<EOL>if not msg:<EOL><INDENT>continue<EOL><DEDENT>log.debug('<STR_LIT>', time.time(), msg, addr)<EOL>self.buffer.put((msg, '<STR_LIT>'....
Handle the connecition with one client.
f2779:c0:m1
def _serve_clients(self):
self.__up = True<EOL>while self.__up:<EOL><INDENT>log.debug('<STR_LIT>')<EOL>try:<EOL><INDENT>conn, addr = self.skt.accept()<EOL>log.debug('<STR_LIT>', addr[<NUM_LIT:0>], addr[<NUM_LIT:1>])<EOL><DEDENT>except socket.error as error:<EOL><INDENT>if not self.__up:<EOL><INDENT>return<EOL><DEDENT>msg = '<STR_LIT>'.format(er...
Accept cients and serve, one separate thread per client.
f2779:c0:m2
def start(self):
log.debug('<STR_LIT>')<EOL>if '<STR_LIT::>' in self.address:<EOL><INDENT>self.skt = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)<EOL><DEDENT>else:<EOL><INDENT>self.skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<EOL><DEDENT>if self.reuse_port:<EOL><INDENT>self.skt.setsockopt(socket.SOL_SOCKET, socket.SO_R...
Start listening for messages.
f2779:c0:m3
def receive(self):
while self.buffer.empty() and self.__up:<EOL><INDENT>sleep_ms = random.randint(<NUM_LIT:0>, <NUM_LIT:1000>)<EOL>time.sleep(sleep_ms / <NUM_LIT>)<EOL><DEDENT>if not self.buffer.empty():<EOL><INDENT>return self.buffer.get(block=False)<EOL><DEDENT>return '<STR_LIT>', '<STR_LIT>'<EOL>
Return one message dequeued from the listen buffer.
f2779:c0:m4
def stop(self):
log.info('<STR_LIT>')<EOL>self.__up = False<EOL>try:<EOL><INDENT>self.skt.shutdown(socket.SHUT_RDWR)<EOL><DEDENT>except socket.error:<EOL><INDENT>log.error('<STR_LIT>', exc_info=True)<EOL><DEDENT>self.skt.close()<EOL>
Closing the socket.
f2779:c0:m5
def get_listener(name):
try:<EOL><INDENT>log.debug('<STR_LIT>', name)<EOL>return LISTENER_LOOKUP[name]<EOL><DEDENT>except KeyError:<EOL><INDENT>msg = '<STR_LIT>'.format(name)<EOL>log.error(msg, exc_info=True)<EOL>raise InvalidListenerException(msg)<EOL><DEDENT>
Return the listener class.
f2780:m0
def start(self):
zmq_uri = '<STR_LIT>'.format(<EOL>protocol=self.protocol,<EOL>address=self.address,<EOL>port=self.port<EOL>) if self.port else'<STR_LIT>'.format( <EOL>protocol=self.protocol,<EOL>address=self.address<EOL>)<EOL>log.debug('<STR_LIT>', zmq_uri)<EOL>self.ctx = zmq.Context()<EOL>if hasattr(zmq, self.type):<EOL><INDENT>skt_...
Startup the zmq consumer.
f2781:c0:m1
def receive(self):
try:<EOL><INDENT>msg = self.sub.recv()<EOL><DEDENT>except zmq.Again as error:<EOL><INDENT>log.error('<STR_LIT>', error, exc_info=True)<EOL>raise ListenerException(error)<EOL><DEDENT>log.debug('<STR_LIT>', time.time(), msg)<EOL>return msg, '<STR_LIT>'<EOL>
Return the message received. ..note:: In ZMQ we are unable to get the address where we got the message from.
f2781:c0:m2
def stop(self):
log.info('<STR_LIT>')<EOL>self.sub.close()<EOL>self.ctx.term()<EOL>
Shutdown zmq listener.
f2781:c0:m3
def start(self):
if '<STR_LIT::>' in self.address:<EOL><INDENT>self.skt = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)<EOL><DEDENT>else:<EOL><INDENT>self.skt = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)<EOL><DEDENT>if self.reuse_port:<EOL><INDENT>self.skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, <NUM_LIT:1>)<EOL>if...
Create the UDP listener socket.
f2782:c0:m1
def receive(self):
try:<EOL><INDENT>msg, addr = self.skt.recvfrom(self.buffer_size)<EOL><DEDENT>except socket.error as error:<EOL><INDENT>log.error('<STR_LIT>', error, exc_info=True)<EOL>raise ListenerException(error)<EOL><DEDENT>log.debug('<STR_LIT>', msg, addr, time.time())<EOL>return msg, addr[<NUM_LIT:0>]<EOL>
Return the message received and the address.
f2782:c0:m2
def stop(self):
log.info('<STR_LIT>')<EOL>self.skt.close()<EOL>
Shut down the UDP listener.
f2782:c0:m3
def _setup_ipc(self):
self.ctx = zmq.Context()<EOL>log.debug('<STR_LIT>', self._name)<EOL>self.sub = self.ctx.socket(zmq.DEALER)<EOL>if six.PY2:<EOL><INDENT>self.sub.setsockopt(zmq.IDENTITY, self._name)<EOL><DEDENT>elif six.PY3:<EOL><INDENT>self.sub.setsockopt(zmq.IDENTITY, bytes(self._name, '<STR_LIT:utf-8>'))<EOL><DEDENT>try:<EOL><INDENT>...
Subscribe to the right topic in the device IPC and publish to the publisher proxy.
f2783:c0:m2
def _compile_messages(self):
self.compiled_messages = []<EOL>if not self._config:<EOL><INDENT>return<EOL><DEDENT>for message_dict in self._config.get('<STR_LIT>', {}):<EOL><INDENT>error = message_dict['<STR_LIT:error>']<EOL>tag = message_dict['<STR_LIT>']<EOL>model = message_dict['<STR_LIT>']<EOL>match_on = message_dict.get('<STR_LIT>', '<STR_LIT>...
Create a list of all OS messages and their compiled regexs
f2783:c0:m3
def _parse(self, msg_dict):
error_present = False<EOL>for message in self.compiled_messages:<EOL><INDENT>match_on = message['<STR_LIT>']<EOL>if match_on not in msg_dict:<EOL><INDENT>continue<EOL><DEDENT>if message['<STR_LIT>'] != msg_dict[match_on]:<EOL><INDENT>continue<EOL><DEDENT>if '<STR_LIT>' in message:<EOL><INDENT>return {<EOL>'<STR_LIT>': ...
Parse a syslog message and check what OpenConfig object should be generated.
f2783:c0:m4
def _emit(self, **kwargs):
oc_dict = {}<EOL>for mapping, result_key in kwargs['<STR_LIT>']['<STR_LIT>'].items():<EOL><INDENT>result = kwargs[result_key]<EOL>oc_dict = napalm_logs.utils.setval(mapping.format(**kwargs), result, oc_dict)<EOL><DEDENT>for mapping, result in kwargs['<STR_LIT>']['<STR_LIT>'].items():<EOL><INDENT>oc_dict = napalm_logs.u...
Emit an OpenConfig object given a certain combination of fields mappeed in the config to the corresponding hierarchy.
f2783:c0:m5
def _publish(self, obj):
bin_obj = umsgpack.packb(obj)<EOL>self.pub.send(bin_obj)<EOL>
Publish the OC object.
f2783:c0:m6
def start(self):
<EOL>napalm_logs_device_messages_received = Counter(<EOL>'<STR_LIT>',<EOL>"<STR_LIT>",<EOL>['<STR_LIT>']<EOL>)<EOL>napalm_logs_device_raw_published_messages = Counter(<EOL>'<STR_LIT>',<EOL>"<STR_LIT>",<EOL>['<STR_LIT>']<EOL>)<EOL>napalm_logs_device_published_messages = Counter(<EOL>'<STR_LIT>',<EOL>"<STR_LIT>",<EOL>['<...
Start the worker process.
f2783:c0:m8
def stop(self):
log.info('<STR_LIT>', self._name)<EOL>self.__up = False<EOL>self.sub.close()<EOL>self.pub.close()<EOL>self.ctx.term()<EOL>
Stop the worker process.
f2783:c0:m9
def get_transport(name):
try:<EOL><INDENT>log.debug('<STR_LIT>', name)<EOL>return TRANSPORT_LOOKUP[name]<EOL><DEDENT>except KeyError:<EOL><INDENT>msg = '<STR_LIT>'.format(name)<EOL>log.error(msg, exc_info=True)<EOL>raise InvalidTransportException(msg)<EOL><DEDENT>
Return the transport class.
f2788:m0
def _exit_gracefully(signum, _):
global _up<EOL>_up = False<EOL>
Called when a signal is caught and marks exiting variable True
f2792:m0
def _suicide_when_without_parent(self, parent_pid):
while True:<EOL><INDENT>time.sleep(<NUM_LIT:5>)<EOL>try:<EOL><INDENT>os.kill(parent_pid, <NUM_LIT:0>)<EOL><DEDENT>except OSError:<EOL><INDENT>self.stop()<EOL>log.warning('<STR_LIT>')<EOL>os._exit(<NUM_LIT>)<EOL><DEDENT><DEDENT>
Kill this process when the parent died.
f2794:c0:m0