Example #1
0
    def emit(self, record):
        priority = SYSLOG_MAP.get(record.levelname, 7)
        message = self.format(record)

        extras = {
            'CODE_FILE': record.pathname,
            'CODE_LINE': record.lineno,
            'CODE_FUNC': record.funcName,
            'THREAD_NAME': record.threadName,
            'PROCESS_NAME': record.processName,
            'LOGGER_NAME': record.name,
            'LOGGER_LEVEL': record.levelname,
            'SYSLOG_IDENTIFIER': self.program_name,
            'PRIORITY': priority
        }

        if record.exc_text:
            extras['EXCEPTION_TEXT'] = record.exc_text

        if record.exc_info:
            extras['EXCEPTION_INFO'] = record.exc_info

        if hasattr(record, "_daiquiri_extra_keys"):
            for k, v in record._daiquiri_extra_keys:
                if k != "_daiquiri_extra_keys":
                    extras[k.upper()] = getattr(record, k)

        journal.send(message, **extras)
def main():
    """ plop """
    # journal.send("New serie name treatment")
    try:
        logmain()
    except Exception as err:
        journal.send("something went wrong for article {}".format(ARTICLE), ERROR=str(err))
Example #3
0
 def on_message(client, userdata, msg):
     journal.send('Got message: ' + msg.payload)
     try:
         cmd = json.loads(msg.payload)
     except ValueError, e:
         journal.send('Invalid JSON received: ' + msg.payload + ', ' + e.message, PRIORITY=journal.LOG_ERR)
         return
Example #4
0
def print_ipv4_event(cpu, data, size):
    event = b["ipv4_events"].event(data)
    global start_ts
    if args.time:
        if args.csv:
            print("%s," % strftime("%H:%M:%S"), end="")
        else:
            print("%-8s " % strftime("%H:%M:%S"), end="")
    if args.timestamp:
        if start_ts == 0:
            start_ts = event.ts_us
        delta_s = (float(event.ts_us) - start_ts) / 1000000
        if args.csv:
            print("%.6f," % delta_s, end="")
        else:
            print("%-9.6f " % delta_s, end="")
    print(format_string %
          (event.skaddr, event.pid, event.task.decode(
              'utf-8', 'replace'), "4" if args.wide or args.csv else "",
           inet_ntop(AF_INET, pack("I", event.saddr)), event.ports >> 16,
           inet_ntop(AF_INET, pack("I", event.daddr)), event.ports & 0xffff,
           tcpstate2str(event.oldstate), tcpstate2str(
               event.newstate), float(event.span_us) / 1000))
    if args.journal:
        journal.send(**journal_fields(event, AF_INET))
Example #5
0
    def stop(self):
        """
        Stop the daemon
        """
        # get the pid from the pidfile
        try:
            pf = file(self.pidfile, 'r')
            pid = int(pf.read().strip())
            pf.close()
        except IOError:
            pid = None

        if not pid:
            message = "pidfile %s does not exist. Daemon not running?\n"
            journal.send(message % self.pidfile)
            return # not an error in a restart

        # Try killing the daemon process
        try:
            while 1:
                os.kill(pid, SIGTERM)
                time.sleep(0.1)
        except OSError, err:
            err = str(err)
            if err.find("No such process") > 0:
                if os.path.exists(self.pidfile):
                    os.remove(self.pidfile)
            else:
                print str(err)
                sys.exit(1)
Example #6
0
    def emit(self, record):
        priority = SYSLOG_MAP.get(record.levelname, 7)
        message = self.format(record)

        extras = {
            'CODE_FILE': record.pathname,
            'CODE_LINE': record.lineno,
            'CODE_FUNC': record.funcName,
            'THREAD_NAME': record.threadName,
            'PROCESS_NAME': record.processName,
            'LOGGER_NAME': record.name,
            'LOGGER_LEVEL': record.levelname,
            'SYSLOG_IDENTIFIER': self.binary_name,
            'PRIORITY': priority
        }

        if record.exc_text:
            extras['EXCEPTION_TEXT'] = record.exc_text

        if record.exc_info:
            extras['EXCEPTION_INFO'] = record.exc_info

        for field in self.custom_fields:
            value = record.__dict__.get(field)
            if value:
                extras[field.upper()] = value

        journal.send(message, **extras)
Example #7
0
def report(e):
    if e['SYSLOG_IDENTIFIER'].startswith("curtin_event"):
        e['SYSLOG_IDENTIFIER'] = event_identifier
        e['CODE_LINE'] = int(e['CODE_LINE'])
        journal.send(**e)
    elif e['SYSLOG_IDENTIFIER'].startswith("curtin_log") and scale_factor < 10:
        print(e['MESSAGE'], flush=True)
Example #8
0
def log_event(name, *arg):
    found = 0
    for i in range(len(content)):
        if name == content[i][EV_NAME]:
            # Found the event in list!
            ev_id = str(content[i][EV_ID])
            severity = content[i][EV_SEVERITY]
            desc = content[i][EV_DESCRIPTION]
            categ = content[i][EV_CATEGORY]
            if len(arg):
                desc = replace_str(arg, desc)
            found = 1
            break
    if found is NOT_FOUND:
        # This means supplied event name is not there in YAML, so return.
        vlog.err("Event not Found")
        return FAIL
    mesg = 'ops-evt|' + ev_id + '|' + severity + '|' + desc
    daemon_name = str(sys.argv[0])
    if "/" in daemon_name:
        daemon_name = daemon_name.split("/")[-1]

    journal.send(mesg,
                 MESSAGE_ID='50c0fa81c2a545ec982a54293f1b1945',
                 PRIORITY=severity,
                 OPS_EVENT_ID=ev_id,
                 OPS_EVENT_CATEGORY=categ,
                 SYSLOG_IDENTIFIER=daemon_name)
Example #9
0
    def emit(self, record):
        """Write record as journal event.

        MESSAGE is taken from the message provided by the
        user, and PRIORITY, LOGGER, THREAD_NAME,
        CODE_{FILE,LINE,FUNC} fields are appended
        automatically. In addition, record.MESSAGE_ID will be
        used if present.
        """
        try:
            msg = self.format(record)
            pri = self.mapPriority(record.levelno)
            mid = getattr(record, 'MESSAGE_ID', None)
            extra = dict(self._extra)
            for key in record.__dict__:
                if key.startswith(JOURNAL_KEY_PREFIX):
                    extra[key[len(JOURNAL_KEY_PREFIX):]] = getattr(record, key)
            send(msg,
                 MESSAGE_ID=mid,
                 PRIORITY=format(pri),
                 LOGGER=record.name,
                 THREAD_NAME=record.threadName,
                 CODE_FILE=record.pathname,
                 CODE_LINE=record.lineno,
                 CODE_FUNC=record.funcName,
                 **extra)
        except Exception:
            self.handleError(record)
Example #10
0
def _copy_lines_to_journal(in_,
                           fields={},
                           n=None,
                           skip=0,
                           terminal_line=""):  # pragma: systemd no cover
    """Copy lines from one file to systemd journal

	Returns None
	"""
    if isinstance(in_,
                  str):  # pragma: no branch - only used with str in test cases
        fin = open(in_, 'r')
    else:
        fin = in_
    # Required for filtering
    fields.update({
        "SYSLOG_IDENTIFIER": "fail2ban-testcases",
        "PRIORITY": "7",
    })
    # Skip
    for i in xrange(skip):
        fin.readline()
    # Read/Write
    i = 0
    while n is None or i < n:
        l = fin.readline()
        if terminal_line is not None and l == terminal_line:
            break
        journal.send(MESSAGE=l.strip(), **fields)
        i += 1
    if isinstance(in_,
                  str):  # pragma: no branch - only used with str in test cases
        # Opened earlier, therefore must close it
        fin.close()
Example #11
0
def _copy_lines_to_journal(in_, fields={},n=None, skip=0, terminal_line=""): # pragma: systemd no cover
	"""Copy lines from one file to systemd journal

	Returns None
	"""
	if isinstance(in_, str): # pragma: no branch - only used with str in test cases
		fin = open(in_, 'r')
	else:
		fin = in_
	# Required for filtering
	fields.update({"SYSLOG_IDENTIFIER": "fail2ban-testcases",
					"PRIORITY": "7",
					})
	# Skip
	for i in xrange(skip):
		fin.readline()
	# Read/Write
	i = 0
	while n is None or i < n:
		l = fin.readline()
		if terminal_line is not None and l == terminal_line:
			break
		journal.send(MESSAGE=l.strip(), **fields)
		i += 1
	if isinstance(in_, str): # pragma: no branch - only used with str in test cases
		# Opened earlier, therefore must close it
		fin.close()
Example #12
0
def index(request):
    startTime = datetime.now()
    journal.send(str(startTime) + " see-em-x Starting")
    StringIO().truncate(0)
    form = json.loads(request.body)
    person = form['person']
    clientCurrent = int(form['clientCurrent'])
    source = form['source']
    clientList = json.loads(form['clientList'])

    clientNext = clientCurrent + 1

    ############ New code section that does parallel API calls to the list of CMX servers. ############
    if not clientList:
        # List where the urls for each request are stored.
        urlClientByUsernameWithDomain = []

        # Add each CMX URL with each username, domain prefix combination to the list.
        for u in urls:
            urlClientByUsernameWithDomain.extend(
                (u + urlClientByUsername + p) for p in prefixes)

        # Dictionary where we will store the responses from CMX in a format of URL:JSON.
        async_dict = {}

        # Use the grequests module to send requests to all the CMX servers at the same time with all domain prefix and username combinations.
        journal.send(
            str(datetime.now() - startTime) + ' Starting async to CMX')

        request = (async .get(url=u + person,
                              auth=HTTPBasicAuth(cmxUser, cmxPass),
                              verify=False,
                              timeout=1)
                   for u in urlClientByUsernameWithDomain)
Example #13
0
    def emit(self, record):
        priority = SYSLOG_MAP.get(record.levelname, 7)
        message = self.format(record)

        extras = {
            "CODE_FILE": record.pathname,
            "CODE_LINE": record.lineno,
            "CODE_FUNC": record.funcName,
            "THREAD_NAME": record.threadName,
            "PROCESS_NAME": record.processName,
            "LOGGER_NAME": record.name,
            "LOGGER_LEVEL": record.levelname,
            "SYSLOG_IDENTIFIER": self.program_name,
            "PRIORITY": priority,
        }

        if record.exc_text:
            extras["EXCEPTION_TEXT"] = record.exc_text

        if record.exc_info:
            extras["EXCEPTION_INFO"] = record.exc_info

        if hasattr(record, "_daiquiri_extra_keys"):
            for k in record._daiquiri_extra_keys:
                if k != "_daiquiri_extra_keys":
                    extras[k.upper()] = getattr(record, k)

        journal.send(message, **extras)
Example #14
0
    def emit(self, record):
        priority = SYSLOG_MAP.get(record.levelname, 7)
        message = self.format(record)

        extras = {
            'CODE_FILE': record.pathname,
            'CODE_LINE': record.lineno,
            'CODE_FUNC': record.funcName,
            'THREAD_NAME': record.threadName,
            'PROCESS_NAME': record.processName,
            'LOGGER_NAME': record.name,
            'LOGGER_LEVEL': record.levelname,
            'SYSLOG_IDENTIFIER': self.binary_name,
            'PRIORITY': priority,
            'SYSLOG_FACILITY': self.facility,
        }

        if record.exc_info:
            # Cache the traceback text to avoid converting it multiple times
            # (it's constant anyway)
            if not record.exc_text:
                record.exc_text = self.formatter.formatException(
                    record.exc_info)
        if record.exc_text:
            extras['EXCEPTION_INFO'] = record.exc_text
            # Leave EXCEPTION_TEXT for backward compatibility
            extras['EXCEPTION_TEXT'] = record.exc_text

        for field in self.custom_fields:
            value = record.__dict__.get(field)
            if value:
                extras[field.upper()] = value

        journal.send(message, **extras)
Example #15
0
 def _log_line_to_systemd(
     self,
     line: str,
     identifier: str = "pepper2-usercode",
 ) -> None:
     """Log a line to the systemd journal."""
     journal.send(line, SYSLOG_IDENTIFIER=identifier)
Example #16
0
    def loop(self):
        #journal.send(MESSAGE="[debug] BeginOffset: " + str(self._offset_begin) + " GroupSize: " + str(self._group_size)
        #                     + " GroupEmpty: " + str(self._group_empty) + " Size: " + str(self._size))

        for position, pixel in self._pixels.items():
            # add empty start pixels
            real_position = position * self._group_size + self._offset_begin

            rgb = colorsys.hsv_to_rgb(h=pixel[0], s=pixel[1], v=pixel[2])

            if real_position > self._real_size:
                journal.send(MESSAGE="[ERROR] real_position: " +
                             str(real_position) + " size: " +
                             str(self._real_size))

            for i in range(0, (self._group_size - self._group_empty)):
                self._strip.setPixelColor(
                    real_position + i,
                    Color(int(rgb[0] * 255), int(rgb[2] * 255),
                          int(rgb[1] * 255)))
            #if any([position < 100,  position > 183]) and pixel[2] < LED_THRESHOLD:
            #    rgb = colorsys.hsv_to_rgb(h=pixel[0], s=pixel[1], v=pixel[2]*LED_CORRECTION)
            #    self._strip.setPixelColor(position, Color(int(rgb[0] * 255 + 1), int(rgb[2] * 255 + 1), int(rgb[1] * 255 + 1)))
            #else:
            #    rgb = colorsys.hsv_to_rgb(h=pixel[0], s=pixel[1], v=pixel[2])
            #    self._strip.setPixelColor(position, Color(int(rgb[0] * 255), int(rgb[2] * 255), int(rgb[1] * 255)))

        self._strip.show()
Example #17
0
    async def curtin_install(self, *, context):
        log.debug('curtin_install')
        self.install_state = InstallState.RUNNING
        self.curtin_event_contexts[''] = context

        journal_fd, watcher = journald_listener(
            [self._event_syslog_identifier, self._log_syslog_identifier],
            self._journal_event)
        self.app.aio_loop.add_reader(journal_fd, watcher)

        curtin_cmd = self._get_curtin_command()

        log.debug('curtin install cmd: {}'.format(curtin_cmd))

        async with self.app.install_lock_file.exclusive():
            try:
                our_tty = os.ttyname(0)
            except OSError:
                # This is a gross hack for testing in travis.
                our_tty = "/dev/not a tty"
            self.app.install_lock_file.write_content(our_tty)
            journal.send("starting install", SYSLOG_IDENTIFIER="subiquity")
            cp = await arun_command(self.logged_command(curtin_cmd),
                                    check=True)

        log.debug('curtin_install completed: %s', cp.returncode)

        self.install_state = InstallState.DONE
        log.debug('After curtin install OK')
Example #18
0
		def test_WrongChar(self):
			self._initFilter()
			self.filter.start()
			# Now let's feed it with entries from the file
			_copy_lines_to_journal(
				self.test_file, self.journal_fields, skip=15, n=4)
			self.assertTrue(self.isFilled(10))
			self.assert_correct_ban("87.142.124.10", 4)
			# Add direct utf, unicode, blob:
			for l in (
		    "error: PAM: Authentication failure for \xe4\xf6\xfc\xdf from 192.0.2.1",
		   u"error: PAM: Authentication failure for \xe4\xf6\xfc\xdf from 192.0.2.1",
		   b"error: PAM: Authentication failure for \xe4\xf6\xfc\xdf from 192.0.2.1".decode('utf-8', 'replace'),
		    "error: PAM: Authentication failure for \xc3\xa4\xc3\xb6\xc3\xbc\xc3\x9f from 192.0.2.2",
		   u"error: PAM: Authentication failure for \xc3\xa4\xc3\xb6\xc3\xbc\xc3\x9f from 192.0.2.2",
		   b"error: PAM: Authentication failure for \xc3\xa4\xc3\xb6\xc3\xbc\xc3\x9f from 192.0.2.2".decode('utf-8', 'replace')
			):
				fields = self.journal_fields
				fields.update(TEST_JOURNAL_FIELDS)
				journal.send(MESSAGE=l, **fields)
			self.assertTrue(self.isFilled(10))
			endtm = MyTime.time()+10
			while len(self.jail) != 2 and MyTime.time() < endtm:
				time.sleep(0.10)
			self.assertEqual(sorted([self.jail.getFailTicket().getIP(), self.jail.getFailTicket().getIP()]), 
				["192.0.2.1", "192.0.2.2"])
Example #19
0
 async def run(self, context):
     env = self.env()
     for i, cmd in enumerate(self.cmds):
         if isinstance(cmd, str):
             desc = cmd
         else:
             desc = ' '.join(cmd)
         with context.child("command_{}".format(i), desc):
             if isinstance(cmd, str):
                 cmd = ['sh', '-c', cmd]
             if self.syslog_id is not None:
                 journal.send("  running " + desc,
                              SYSLOG_IDENTIFIER=self.syslog_id)
                 cmd = [
                     'systemd-cat',
                     '--level-prefix=false',
                     '--identifier=' + self.syslog_id,
                 ] + cmd
             await arun_command(cmd,
                                env=env,
                                stdin=None,
                                stdout=None,
                                stderr=None,
                                check=self.cmd_check)
     self.run_event.set()
Example #20
0
 def _maybe_push_to_journal(self, event_type, context, description):
     if not context.get('is-install-context') and self.interactive():
         controller = context.get('controller')
         if controller is None or controller.interactive():
             return
     if context.get('request'):
         return
     indent = context.full_name().count('/') - 2
     if context.get('is-install-context') and self.interactive():
         indent -= 1
         msg = context.description
     else:
         msg = context.full_name()
         if description:
             msg += ': ' + description
     msg = '  ' * indent + msg
     if context.parent:
         parent_id = str(context.parent.id)
     else:
         parent_id = ''
     journal.send(msg,
                  PRIORITY=context.level,
                  SYSLOG_IDENTIFIER=self.event_syslog_id,
                  SUBIQUITY_CONTEXT_NAME=context.full_name(),
                  SUBIQUITY_EVENT_TYPE=event_type,
                  SUBIQUITY_CONTEXT_ID=str(context.id),
                  SUBIQUITY_CONTEXT_PARENT_ID=parent_id)
Example #21
0
	def run(self):
		#rule = None
		if self.rules == [] or self.rules == None:
			return

		#for rule in self.rules:
			#print(rule)
			#break
			#print("Journald_watcher.run -> {}".format(rule.getRulename()))

		#print("Processing {}".format(rule.getNameOfBelongService()))

		p = select.poll()
		p.register(self.j, self.j.get_events())

		if self.retroactive == True:
			# Send a message to journald to generate a I/O traffic
			# so that poll catches events in the past without having
			# to wait for journal.APPEND to occur naturally.
			journal.send("GGH")

		# Inspiration taken from https://yalis.fr/git/yves/pyruse/src/branch/master/pyruse/main.py
		# In accordance with § 31 odst. 1 písm. a) zákona č. 121/2000 Sb., autorský zákon
		while p.poll():
			#print(self.j.process())
			if self.j.process() != journal.APPEND:
				continue
			for entry in self.j:
				if entry['MESSAGE'] != "":
					# Print SYSTEMD messages for debugging
					#print(str(entry['__REALTIME_TIMESTAMP'])+ ' ' + entry['MESSAGE'])
					#print()
					for rule in self.rules:
						self.processRule(rule, entry['MESSAGE'])
		pass
Example #22
0
    def emit(self, record):
        """Write `record` as a journal event.

        MESSAGE is taken from the message provided by the user, and PRIORITY,
        LOGGER, THREAD_NAME, CODE_{FILE,LINE,FUNC} fields are appended
        automatically. In addition, record.MESSAGE_ID will be used if present.

        This also records all the extra data fetched by `get_extra_data`.
        """
        try:
            extra_data = flatten(get_extra_data(record))
            extra_data = {(EXTRA_LOGDATA_PREFIX + key).upper():
                          stringify(value)
                          for key, value in extra_data}
            msg = self.format(record)
            pri = self.mapPriority(record.levelno)
            send(
                msg,
                PRIORITY=format(pri),
                LOGGER=record.name,
                THREAD_NAME=record.threadName,
                CODE_FILE=record.pathname,
                CODE_LINE=record.lineno,
                CODE_FUNC=record.funcName,
                **extra_data,
            )
        except Exception:
            self.handleError(record)
Example #23
0
    def emit(self, record):
        """Write record as journal event.

        MESSAGE is taken from the message provided by the
        user, and PRIORITY, LOGGER, THREAD_NAME,
        CODE_{FILE,LINE,FUNC} fields are appended
        automatically. In addition, record.MESSAGE_ID will be
        used if present.
        """
        try:
                msg = self.format(record)
                pri = self.mapPriority(record.levelno)
                mid = getattr(record, 'MESSAGE_ID', None)
                extra = dict(self._extra)
                for key in record.__dict__:
                    if key.startswith(JOURNAL_KEY_PREFIX):
                        extra[key[len(JOURNAL_KEY_PREFIX):]] = getattr(record, key)
                send(msg,
                     MESSAGE_ID=mid,
                     PRIORITY=format(pri),
                     LOGGER=record.name,
                     THREAD_NAME=record.threadName,
                     CODE_FILE=record.pathname,
                     CODE_LINE=record.lineno,
                     CODE_FUNC=record.funcName,
                     **extra)
        except Exception:
                self.handleError(record)
Example #24
0
    async def curtin_install(self, *, context):
        log.debug('curtin_install')
        self.curtin_event_contexts[''] = context

        loop = self.app.aio_loop

        fds = [
            journald_listen(loop, [self.app.log_syslog_id], self.curtin_log),
            journald_listen(loop, [self._event_syslog_id], self.curtin_event),
        ]

        curtin_cmd = self._get_curtin_command()

        log.debug('curtin install cmd: {}'.format(curtin_cmd))

        async with self.app.install_lock_file.exclusive():
            try:
                our_tty = os.ttyname(0)
            except OSError:
                # This is a gross hack for testing in travis.
                our_tty = "/dev/not a tty"
            self.app.install_lock_file.write_content(our_tty)
            journal.send("starting install", SYSLOG_IDENTIFIER="subiquity")
            try:
                cp = await arun_command(self.logged_command(curtin_cmd),
                                        check=True)
            finally:
                for fd in fds:
                    loop.remove_reader(fd)

        log.debug('curtin_install completed: %s', cp.returncode)
Example #25
0
 def event(self, name, metadata):
     message = "Recording event '{0}'".format(
         name)  #just not required but lazy to deal with `sendv` api
     journal.send(message,
                  PRIORITY=journal.LOG_INFO,
                  LOGGER=self._name,
                  EVENT_NAME=name,
                  **(Logger._format_metadata(metadata)))
Example #26
0
 def OnTopic(self, user, channel, message):
     try:
         self.insert("TOPIC", channel.GetName(), user.GetHost(),
                     user.GetNick(), None, self.timestamp(), None,
                     message.s, str(self.GetNetwork()))
     except Exception as e:
         journal.send(repr(e))
     return True
Example #27
0
 def OnDevoice(self, user, target_user, channel, noChange):
     try:
         self.insert("DEVOICE", channel.GetName(), user.GetHost(),
                     user.GetNick(), None, self.timestamp(),
                     target_user.GetNick(), None, str(self.GetNetwork()))
     except Exception as e:
         journal.send(repr(e))
     return True
Example #28
0
 def OnJoin(self, user, channel):
     try:
         self.insert("JOIN", channel.GetName(), user.GetHost(),
                     user.GetNick(), None, self.timestamp(), None, None,
                     str(self.GetNetwork()))
     except Exception as e:
         journal.send(repr(e))
     return True
Example #29
0
 def OnKick(self, user, target_nick, channel, message):
     try:
         self.insert("KICK", channel.GetName(), user.GetHost(),
                     user.GetNick(), None, self.timestamp(), target_nick,
                     message, str(self.GetNetwork()))
     except Exception as e:
         journal.send(repr(e))
     return True
Example #30
0
 def _log_debug(self, message):
     """Logging messages"""
     if self._debug:
         log_msg = self.name() + ", " + message
         if use_journal:
             journal.send(log_msg, PRIORITY=7, SYSLOG_IDENTIFIER="sspl-ll")
         else:
             logger.info(log_msg)
Example #31
0
 def log_status(self, message, message_id):
     "Log directly to the journal"
     journal.send(message,
                  MESSAGE_ID=message_id,
                  PRIORITY=journal.LOG_NOTICE,
                  SYSTEM_RELEASEVER=self.state.system_releasever,
                  TARGET_RELEASEVER=self.state.target_releasever,
                  DNF_VERSION=dnf.const.VERSION)
 def log_status(self, message, message_id):
     "Log directly to the journal"
     journal.send(message,
                  MESSAGE_ID=message_id,
                  PRIORITY=journal.LOG_NOTICE,
                  SYSTEM_RELEASEVER=self.state.system_releasever,
                  TARGET_RELEASEVER=self.state.target_releasever,
                  DNF_VERSION=dnf.const.VERSION)
Example #33
0
    def followCommand(self, cmd, fromIden=None):
        '''Do job as required.'''
	journal.send('Follow command: ' + json.dumps(cmd))
        if cmd['command'] == 'get_status':
            pass
        elif cmd['command'] == 'start_motion':
            os.system('systemctl start motion')
        elif cmd['command'] == 'stop_motion':
            os.system('systemctl stop motion')
Example #34
0
 def OnQuit(self, user, message, channels):
     try:
         for channel in channels:
             self.insert("QUIT", channel.GetName(), user.GetHost(),
                         user.GetNick(), None, self.timestamp(), None,
                         message, str(self.GetNetwork()))
     except Exception as e:
         journal.send(repr(e))
     return True
Example #35
0
 def OnNick(self, user, new_nick, channels):
     try:
         for channel in channels:
             self.insert("NICK", channel.GetName(), user.GetHost(),
                         user.GetNick(), None, self.timestamp(), new_nick,
                         None, str(self.GetNetwork()))
     except Exception as e:
         journal.send(repr(e))
     return True
Example #36
0
 def OnChanMsg(self, user, channel, message):
     try:
         self.insert("SAY", channel.GetName(), user.GetHost(),
                     user.GetNick(), self.findMode(channel, user),
                     self.timestamp(), None, message.s,
                     str(self.GetNetwork()))
     except Exception as e:
         journal.send(repr(e))
     return True
Example #37
0
    def safe_journal_sender(MESSAGE: str, **kwargs) -> None:

        MESSAGE = sanitize_string(MESSAGE)

        for key, value in kwargs.items():
            if isinstance(value, str):
                kwargs[key] = sanitize_string(value)

        journal.send(MESSAGE, **kwargs)
Example #38
0
def message(message, verbosity, systemd=systemd):
    """This function is used by others for message printing.

    Args:
        message (string): The text used for logging messages.
        verbosity (int): If set to be >= ``1`` it will print out the string passed to ``message()``
        systemd (bool): If not ``False`` (the system uses the systemd journal), it will log to it using ``message``.
    """
    if verbosity >= 1:
        print("==> " + message)
    if systemd is not False:
        journal.send(message + ".", SYSLOG_IDENTIFIER="archmap")
    def daemonize(self):
        """
        do the UNIX double-fork magic, see Stevens' "Advanced Programming in the
        UNIX Environment" for details (ISBN 0201563177)
        http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
        """
        try:
            pid = os.fork()
            if pid > 0:
                #exit first parent
                sys.exit(0)
        except OSError, e:
            message = "fork #1 failed: %d (%s)\n"
	    journal.send(message % (e.errno, e.strerror))
            sys.exit(1)
    def start(self):
        """
        Start the daemon
        """
        # Check for a pidfile to see if the daemon already runs
        try:
            pf = file(self.pidfile, 'r')
            pid = int(pf.read().strip())
            pf.close()
        except IOError:
            pid = None

        if pid:
            message = "pidfile %s already exists. Daemon already running?\n"
            journal.send(message % self.pidfile)
            sys.exit(1)

        # Start the Daemon
        self.daemonize()
        self.run()
def log():

    try:
        data = loads(request.data)
        if 'MESSAGE' in data:
            msg = data.get('MESSAGE')
            del data['MESSAGE']
        else:
            msg = ''
        data['REMOTE_ADDR'] = ','.join(
            [ip
             for ip in request.environ.get('HTTP_X_FORWARDED_FOR', '').split(',')
             if not ip.startswith('172.')]
        )
        if request.environ.get('REMOTE_ADDR', '') and data['REMOTE_ADDR'] == '':
            data['REMOTE_ADDR'] += request.environ.get('REMOTE_ADDR', '')
        data['SYSLOG_IDENTIFIER'] = 'AUCTION_CLIENT'
        send(msg, **data)
        return Response('ok')
    except:
        return Response('error')
Example #42
0
 def on_push(msg):
     journal.send('Got message: ' + json.dumps(msg))
     try:
         pushes = self.pb.get_pushes(self.lastFetch)
         journal.send('Got pushes: ' + json.dumps(pushes))
         self.lastFetch = time.time()
         if type(pushes) is types.TupleType and len(pushes)>1 \
                 and type(pushes[1]) is types.ListType:
             for push in pushes[1]:
                 journal.send('Check push: ' + json.dumps(push))
                 if push.has_key('target_device_iden') and push['target_device_iden'] == deviceIden:
                     cmd = json.loads(push['body'])
                     self.home.followCommand(cmd)
                     fromIden = push['source_device_iden']
                     fromDevice = self.getDevice(fromIden)
                     if fromDevice is None:
                         journal.send('get_status: Cannot find device with iden "' + fromIden + '"', PRIORITY=journal.LOG_ERR)
                         return
                     self.pb.push_note('status', self.home.getStatus(), fromDevice)
     except (PushbulletError, IOError, ValueError, KeyError), e:
         journal.send(str(e), PRIORITY=journal.LOG_ERR)
Example #43
0
def print_ipv6_event(cpu, data, size):
    event = b["ipv6_events"].event(data)
    global start_ts
    if args.time:
        if args.csv:
            print("%s," % strftime("%H:%M:%S"), end="")
        else:
            print("%-8s " % strftime("%H:%M:%S"), end="")
    if args.timestamp:
        if start_ts == 0:
            start_ts = event.ts_us
        delta_s = (float(event.ts_us) - start_ts) / 1000000
        if args.csv:
            print("%.6f," % delta_s, end="")
        else:
            print("%-9.6f " % delta_s, end="")
    print(format_string % (event.skaddr, event.pid, event.task.decode('utf-8', 'replace'),
        "6" if args.wide or args.csv else "",
        inet_ntop(AF_INET6, event.saddr), event.ports >> 32,
        inet_ntop(AF_INET6, event.daddr), event.ports & 0xffffffff,
        tcpstate2str(event.oldstate), tcpstate2str(event.newstate),
        float(event.span_us) / 1000))
    if args.journal:
        journal.send(**journal_fields(event, AF_INET6))
Example #44
0
 def on_connect(client, userdata, flags, rc):
     journal.send("Connected with result code "+str(rc))
     self.mq.subscribe(self.cfg.get('mosquitto', 'topic_client'))
Example #45
0
#!/usr/bin/env python

## Write to the systemd journal
##
## watch the journal in another window with:
##
##   sudo journalctl -f  {--output=json}
##

from systemd import journal

# simple message send
#
journal.send("a systemd-journal log message from python land.")

# send message w/ extra fields.
#
journal.send( "A systemd-journal log message from python land, with extra field",
              EXTRAFIELD='foo field')

# sendv() is also supported
#
journal.sendv( 'MESSAGE=systemd-journal log message using sendv() from python',
               'EXTRAFIELD=foo field' )

# a very syslog like message
#
journal.send( 'Principal log message (from python land)',
              SYSLOG_FACILITY='USER',
              SYSLOG_IDENTIFIER='py-write-journal',
              SYSLOG_PID=-1,
Example #46
0
                            rdb = redis.StrictRedis(host='localhost', port=6379, db=1, decode_responses=True)
                            ckey = rdb.get("CarbonKey")
                            lkey = rdb.get("ListBoat")

                            r = requests.post("https://www.carbonitex.net/discord/data/botdata.php",
                                              data=json.dumps({'key': ckey, 'servercount': total_stats['Servers']}),
                                              headers={'Content-Type': 'application/json'})
                            if r.status_code != 200:
                                journal.send("ERROR:AngelBot Manager: Attempted to update carbonitex but got {}".format(r.status_code))
                                journal.send("ERROR DETAILS: {}".format(r.text))

                            r = requests.post("https://bots.discord.pw/api/bots/168925517079248896/stats",
                                              data=json.dumps({'server_count': total_stats['Servers']}),
                                              headers={"Authorization": lkey, "Content-Type": "application/json"})
                            if r.status_code != 200:
                                journal.send("ERROR:AngelBot Manager: Attempted to update bots.discord.pw but got {}".format(r.status_code))
                                journal.send("ERROR DETAILS: {}".format(r.text))

                            rdb.hset("stats", "users", total_stats['Users'])
                            rdb.hset("stats", "servers", total_stats['Servers'])
                            journal.send("INFO:AngelBot Manager: Finished updating stats.")
                            for shard in range(self.shard_count):
                                del self.shards[shard]['stats']

        journal.send("INFO:AngelBot Manager: Shards Exhausted. Shutting down.")
        
    
if __name__ == "__main__":
    journal.send("STATUS:AngelBot Manager: Starting Up.")
    MPManager()
 def log_message(self, msg):
     journal.send(msg, SYSLOG_IDENTIFIER='logemperor-test')
     # wait for log message to process
     time.sleep(0.2)
Example #48
0
    def print(self, msg):
        print(msg)

        if self.use_journal:
            journal.send(msg)
             )
    parser.add_argument('command',
                        action='store',
                        choices=['start','stop','restart','debug'],
                        help='What to do. Use debug to start in the foreground')
    args = parser.parse_args()

    # Parse the config file
    config = ConfigParser.ConfigParser(allow_no_value=True)
    if(args.config):
        confok = config.read(args.config)
    else:
        confok = config.read(['/etc/conf.d/watcher.ini']);

    if(not confok):
        journal.send("Failed to read config file. Try -c parameter\n")
        sys.exit(4);

    # Initialize the daemon
    daemon = WatcherDaemon(config)

    # Execute the command
    if 'start' == args.command:
        daemon.start()
    elif 'stop' == args.command:
        daemon.stop()
    elif 'restart' == args.command:
        daemon.restart()
    elif 'debug' == args.command:
        daemon.run()
    else:
Example #50
0
 def send(self, article):
     """Write an article to the blog"""
     journal.send(article.content,
         ARTICLE_TITLE=article.title,
         ARTICLE_TAGS=', '.join(article.tags),
         SYSLOG_IDENTIFIER='systemd-blog')
#!/usr/bin/python2

import requests
import subprocess
from systemd import journal

SPARQL = "http://lod.cedar-project.nl/cedar/sparql"
TEST_QUERY = "SELECT * WHERE {?s ?p ?o} LIMIT 1"
TEST_RESOURCE = "http://lod.cedar-project.nl:8888/cedar/resource/harmonised-data-dsd"

if __name__ == '__main__':
    # Check virtuoso
    r = requests.post(SPARQL, data={'query':TEST_QUERY})
    if r.status_code != 200:
        systemd.journal.send('Restart Virtuoso')
        subprocess.call("systemctl restart virtuoso", shell=True)
    else:
        journal.send('Virtuoso is doing fine')
        
    # Check tomcat
    r = requests.get(TEST_RESOURCE)
    if r.status_code != 200:
        systemd.journal.send('Restart Tomcat')
        subprocess.call("systemctl restart tomcat6", shell=True)
        systemd.journal.send('Restart Apache')
        subprocess.call("systemctl restart httpd", shell=True)
    else:
        journal.send('Tomcat is doing fine')
    
def log(msg):
    journal.send(msg)
 def __call__(self, data):
     journal.send(
         data['data'],
         CIRCUS_STREAM=data['name'],
         **self.kwargs
     )
Example #54
0
#!/usr/bin/env python
from sys import argv
from systemd import journal
journal.send('deluge test: the script started running')
for arg in argv[1:]:
    journal.send(arg)
Example #55
0
    def start_shards(self):
        for number in range(self.shard_count):
            r, w = Pipe(duplex=False)
            self.r_pipes.append(r)
            journal.send("INFO:AngelBot Manager: Starting Shard {}".format(number))
            temp = Process(target=self._run, args=(number, self.shard_count, w), daemon=False)
            temp.start()
            self.shards[number] = {'Pipe': r, 'Process': temp}
            journal.send("STATUS:Shard {}: Shard Started.".format(number))

        while self.shards.keys():
            for shard in self.shards.keys():
                if not self.shards[shard]['Process'].is_alive():
                    del self.shards[shard]
                    journal.send("QUIT:Shard {0}: Shard {0} Exited.".format(shard))
                    journal.send("INFO:AngelBot Manage: Attempting to restart Shard {}".format(shard))
                    r, w = Pipe(duplex=False)
                    self.r_pipes.append(r)
                    journal.send("INFO:AngelBot Manager: Starting Shard {}".format(shard))
                    temp = Process(target=self._run, args=(shard, self.shard_count, w), daemon=False)
                    temp.start()
                    self.shards[shard] = {'Pipe': r, 'Process': temp}
                    journal.send("STATUS:Shard {}: Shard restarted.".format(shard))
                    
            for reader in wait(self.r_pipes):
                try:
                    msg = reader.recv()
                except EOFError:
                    self.r_pipes.remove(reader)
                else:
                    if 'QUIT' in msg:
                        shard = int(msg.split(":")[1])
                        if shard in self.shards:
                            del self.shards[shard]
                            journal.send("QUIT:Shard {0}: Shard {0} Exited.".format(shard))
                            journal.send("INFO:AngelBot Manager: Attempting to restart Shard {}".format(shard))
                            r, w = Pipe(duplex=False)
                            self.r_pipes.append(r)
                            journal.send("INFO:AngelBot Manager: Starting Shard {}".format(shard))
                            temp = Process(target=self._run, args=(shard, self.shard_count, w), daemon=False)
                            temp.start()
                            self.shards[shard] = {'Pipe': r, 'Process': temp}
                            journal.send("STATUS:Shard {}: Shard restarted.".format(shard))
                    elif 'STATUS' in msg:
                        _, sid, servs, members = msg.split(":")
                        sid = int(sid)
                        journal.send("UPDATE:Shard {0}: Shard {0} reporting stats. {1} servers. {2} members.".format(sid,
                                                                                                                     servs,
                                                                                                                     members))
                        self.shards[sid]['stats'] = {'servers': int(servs), 'users': int(members)}

                        if all('stats' in self.shards[x] for x in range(self.shard_count)):
                            total_stats = {'Servers': sum(self.shards[x]['stats']['servers'] for x in range(self.shard_count)),
                                           'Users': sum(self.shards[x]['stats']['users'] for x in range(self.shard_count))}

                            rdb = redis.StrictRedis(host='localhost', port=6379, db=1, decode_responses=True)
                            ckey = rdb.get("CarbonKey")
                            lkey = rdb.get("ListBoat")

                            r = requests.post("https://www.carbonitex.net/discord/data/botdata.php",
                                              data=json.dumps({'key': ckey, 'servercount': total_stats['Servers']}),
                                              headers={'Content-Type': 'application/json'})
                            if r.status_code != 200:
                                journal.send("ERROR:AngelBot Manager: Attempted to update carbonitex but got {}".format(r.status_code))
                                journal.send("ERROR DETAILS: {}".format(r.text))

                            r = requests.post("https://bots.discord.pw/api/bots/168925517079248896/stats",
                                              data=json.dumps({'server_count': total_stats['Servers']}),
                                              headers={"Authorization": lkey, "Content-Type": "application/json"})
                            if r.status_code != 200:
                                journal.send("ERROR:AngelBot Manager: Attempted to update bots.discord.pw but got {}".format(r.status_code))
                                journal.send("ERROR DETAILS: {}".format(r.text))

                            rdb.hset("stats", "users", total_stats['Users'])
                            rdb.hset("stats", "servers", total_stats['Servers'])
                            journal.send("INFO:AngelBot Manager: Finished updating stats.")
                            for shard in range(self.shard_count):
                                del self.shards[shard]['stats']

        journal.send("INFO:AngelBot Manager: Shards Exhausted. Shutting down.")
#!/usr/bin/env python
import fileinput
from systemd import journal
import re
import sys
import os


identifier = os.environ.get('CONTAINER')
pattern = re.compile('^<([0-9])>(.*)$')

for line in fileinput.input():
    line = line.strip()

    match = pattern.search(line)

    if match is not None:
        priority = int(match.group(1))
        message = match.group(2)
    else:
        priority = 6
        message = line

    journal.send(message, PRIORITY=priority, CONTAINER=identifier, SYSLOG_IDENTIFIER=identifier)
Example #57
0
            journal.send('Got message: ' + msg.payload)
            try:
                cmd = json.loads(msg.payload)
            except ValueError, e:
                journal.send('Invalid JSON received: ' + msg.payload + ', ' + e.message, PRIORITY=journal.LOG_ERR)
                return
            self.home.followCommand(cmd)
            self.mq.publish(self.cfg.get('mosquitto', 'topic_msg'), self.home.getStatus(), 2)
        self.mq.on_message = on_message

    def run(self):
        self.mq.connect(self.cfg.get('mosquitto', 'host'), 1883, 60)
        self.mq.loop_forever()


if __name__ == '__main__':
    try:
        cfg = ConfigParser()
        cfg.read(CONFIG_FILE)
        cfg.get('global', 'api_key')

        r = MosquittoRoute(cfg)
        r.run()
    except (NoSectionError, NoOptionError), e:
        err = 'Config file is missing or invalid: ' + str(e)
        journal.send(err, PRIORITY=journal.LOG_ERR)
        sys.stderr.write(err + "\n")
        sys.exit(1)
    except KeyboardInterrupt:
        print('Game over.')
Example #58
0
def log (str):

	print(str)
	journal.send(str)
Example #59
0
 async def on_error(self, event, *args, **kwargs):
     if event == "on_message":
         journal.send("Encountered an exception in a command.\nException Type: {}\nCommand: {}\nArguments: {}\nEntire Line: {}\n{}".format(sys.exc_info()[0], args[0].content[:re.search("\s",args[0].content).start()], args[0].content[re.search("\s",args[0].content).end():], args[0].content, '\n'.join(traceback.format_tb(sys.exc_info()[2]))))
     else:
         journal.send("Ignoring exception in {}:{}\n{}".format(event, sys.exc_info()[0], traceback.format_tb(sys.exc_info()[2])))