Skip to content

Commit

Permalink
add logger sources
Browse files Browse the repository at this point in the history
  • Loading branch information
fliepeltje committed Mar 11, 2024
1 parent f64b6c5 commit a2daa62
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
1 change: 0 additions & 1 deletion humitifier/alerts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Literal
from humitifier.facts import SSH_FACTS
from humitifier.utils import FactError

SEVERITY = Literal["info", "warning", "critical"]

Expand Down
3 changes: 2 additions & 1 deletion humitifier/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from humitifier.logging import logging

CONF_FILE = os.environ.get("HUMITIFIER_CONFIG", ".local/app_config.toml")
logger = logging.getLogger(__name__)


@dataclass
Expand All @@ -24,7 +25,7 @@ def load(cls) -> "Config":

inventory_set = set(data["inventory"])
if len(inventory_set) != len(data["inventory"]):
logging.warning("Duplicate entries in inventory")
logger.warning("Duplicate entries in inventory")
data["inventory"] = list(inventory_set)
return cls(**data)

Expand Down
8 changes: 6 additions & 2 deletions humitifier/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@

import sentry_sdk

logger = logging.getLogger(__name__)

if sentry_dsn := os.getenv("SENTRY_DSN"):
logging.info("Sentry enabled")
sentry_sdk.init(dsn=sentry_dsn, traces_sample_rate=1.0, profiles_sample_rate=1.0)


template_env = Environment(loader=FileSystemLoader("humitifier/templates"))
template_env.filters["json"] = lambda x: json.dumps(x, indent=4, sort_keys=True, separators=(",", ": "))
app = FastAPI()
Expand Down Expand Up @@ -119,9 +123,9 @@ async def index(request: Request):

@app.on_event("startup")
async def run_migrations():
logging.info("Applying migrations...")
logger.info("Applying migrations...")
conn = await asyncpg.connect(CONFIG.db)
for f in os.listdir(CONFIG.migrations_dir):
logging.info(f"Applying {f}")
logger.info(f"Applying {f}")
with open(f"{CONFIG.migrations_dir}/{f}") as sql_file:
await conn.execute(sql_file.read())
4 changes: 3 additions & 1 deletion humitifier/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from humitifier.logging import logging
from humitifier.utils import FactError

logger = logging.getLogger(__name__)


@dataclass
class Facts:
Expand All @@ -32,7 +34,7 @@ def from_sql_rows(cls, rows):
parser = getattr(facts, name)
create_args[name] = parser.from_sql(data)
except Exception as e:
logging.error(f"Error parsing {name}: {e}")
logger.info(f"Error parsing {name}: {e}")
create_args[name] = FactError(**data)
return cls(**create_args)

Expand Down
12 changes: 7 additions & 5 deletions humitifier/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from humitifier.logging import logging
from humitifier.utils import FactError

logger = logging.getLogger(__name__)

app = Rocketry(execution="async")


Expand All @@ -35,7 +37,7 @@ def parse_row_data(row) -> facts.SshFact | FactError:
try:
return cls.from_stdout(row["stdout"].split("\n"))
except Exception as e:
logging.error(f"Error parsing {row['name']}: {e}\nData: {row}")
logger.warning(f"Error parsing {row['name']}: {e}\nData: {row}")
return FactError(
stdout=row["stdout"],
stderr=row["stderr"],
Expand All @@ -48,23 +50,23 @@ def parse_row_data(row) -> facts.SshFact | FactError:
@app.task(hourly)
async def sync_hosts():
time.sleep(2)
logging.info("Syncing hosts")
logger.info("Syncing hosts")
conn = await asyncpg.connect(CONFIG.db)
await conn.execute("""SELECT sync_hosts($1)""", CONFIG.inventory)
await conn.close()


@app.task(after_success(sync_hosts))
async def scan_hosts():
logging.info("Initiating scan of hosts")
logger.info("Initiating scan of hosts")
ts = datetime.now()
conn = await asyncpg.connect(CONFIG.db)
await conn.execute("""INSERT INTO scans(ts) VALUES ($1)""", ts)

fact_outputs = []

for fact in facts.SSH_FACTS:
logging.info(f"Querying {fact.__name__}...")
logger.info(f"Querying {fact.__name__}...")
fact_outputs.extend(collect_outputs(fact.__name__, ts, fact.cmd))
await conn.executemany(
"""INSERT INTO host_outputs(name, host, scan, stdout, stderr, exception, exit_code)
Expand All @@ -87,7 +89,7 @@ async def scan_hosts():

@app.task(after_success(scan_hosts))
async def parse_facts():
logging.info("Parsing facts")
logger.info("Parsing facts")
conn = await asyncpg.connect(CONFIG.db)
rows = await conn.fetch("""SELECT name, host, scan, stdout, stderr, exception, exit_code FROM host_outputs""")
parsed_rows = [(row["name"], row["host"], row["scan"], parse_row_data(row)) for row in rows]
Expand Down

0 comments on commit a2daa62

Please sign in to comment.