sysview-py 0.4.6__tar.gz → 0.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: sysview-py
3
- Version: 0.4.6
3
+ Version: 0.5.0
4
4
  Summary: Add your description here
5
5
  Author: Michael Wilson
6
6
  Author-email: Michael Wilson <mw@1wilson.org>
@@ -0,0 +1,21 @@
1
+ [project]
2
+ name = "sysview-py"
3
+ version = "0.5.0"
4
+ description = "Add your description here"
5
+ readme = "README.md"
6
+ requires-python = ">=3.12"
7
+ dependencies = ["jinja2>=3.1.6"]
8
+
9
+ [[project.authors]]
10
+ name = "Michael Wilson"
11
+ email = "mw@1wilson.org"
12
+
13
+ [project.scripts]
14
+ sysview-py = "sysview_py.main:main"
15
+
16
+ [build-system]
17
+ requires = ["uv_build>=0.10.4,<0.11.0"]
18
+ build-backend = "uv_build"
19
+
20
+ [dependency-groups]
21
+ dev = ["ruff>=0.15.2"]
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sysview-py"
3
- version = "0.4.6"
3
+ version = "0.5.0"
4
4
  description = "Add your description here"
5
5
  readme = "README.md"
6
6
  authors = [
@@ -9,7 +9,7 @@ class SysviewDB:
9
9
  CREATE TABLE types(name, UNIQUE(name));
10
10
  CREATE TABLE hosts_types(host, type, UNIQUE(host, type));
11
11
  CREATE TABLE reports(hostname, type, date, raw_report);
12
- CREATE TABLE items(command, status, status_text, additional_text, raw_item, hostname, report_id);
12
+ CREATE TABLE items(command, status, status_text, additional_text, raw_item, hostname, report_id, report_type);
13
13
  """
14
14
 
15
15
  def __init__(self, path):
@@ -18,9 +18,7 @@ class SysviewDB:
18
18
  self._cursor = None
19
19
 
20
20
  def connect(self):
21
- db_exists = False
22
- if self.path.exists():
23
- db_exists = True
21
+ db_exists = self.path.exists()
24
22
  self._connection = sqlite3.connect(self.path)
25
23
  self._cursor = self._connection.cursor()
26
24
  if not db_exists:
@@ -82,20 +80,21 @@ class SysviewDB:
82
80
  i.raw_item,
83
81
  report.hostname,
84
82
  report_id,
83
+ report.type,
85
84
  )
86
85
  for i in report.items
87
86
  ]
88
- self._cursor.executemany("INSERT INTO items VALUES(?, ?, ?, ?, ?, ?, ?)", items)
87
+ self._cursor.executemany("INSERT INTO items VALUES(?, ?, ?, ?, ?, ?, ?, ?)", items)
89
88
 
90
89
  self._connection.commit()
91
90
 
92
91
  def get_last_changed_item(self, item):
93
- sql = f"SELECT report_id,raw_item FROM items WHERE command = '{item.command}' AND hostname = '{item.host}' AND report_id < {item.report_id} AND NOT status = '{item.status}' ORDER BY rowid DESC LIMIT 1"
92
+ sql = f"SELECT report_id,raw_item FROM items WHERE command = '{item.command}' AND hostname = '{item.host}' AND report_type = '{item.report_type}' AND report_id < {item.report_id} AND NOT status = '{item.status}' ORDER BY rowid DESC LIMIT 1"
94
93
  last_changed_item = self._cursor.execute(sql).fetchone()
95
94
  if last_changed_item is not None:
96
95
  report_id = last_changed_item[0]
97
96
  raw_item = last_changed_item[1]
98
- last_changed_item = ReportItem(raw_item, report_id=report_id)
97
+ last_changed_item = ReportItem(raw_item, report_id=report_id, is_history_item=True)
99
98
  last_changed_item.parse()
100
99
  sql = f"SELECT date FROM reports WHERE rowid = '{last_changed_item.report_id}'"
101
100
  report_date = datetime.datetime.fromtimestamp(
@@ -106,7 +105,7 @@ class SysviewDB:
106
105
  return last_changed_item
107
106
 
108
107
  def get_item_history(self, item, max_len=10):
109
- history = []
108
+ history = [item]
110
109
  last_item = item
111
110
  while last_item is not None and len(history) < max_len:
112
111
  last_item = self.get_last_changed_item(last_item)
@@ -5,11 +5,14 @@ from .sysview import Sysview
5
5
  from .flock import lock_file
6
6
 
7
7
  LOCK_FILE = Path.home() / ".sysview.lock"
8
+ HTML_ROOT = Path.home() / "sysview"
8
9
 
9
10
 
10
11
  def main():
11
12
  with lock_file(LOCK_FILE):
12
- sysview = Sysview()
13
+ sysview = Sysview(html_root=HTML_ROOT)
14
+ if not sysview.html_root.exists():
15
+ sysview.html_root.mkdir()
13
16
  db_path = Path.home() / ".sysview.db"
14
17
  sysview.setup_db(db_path)
15
18
  reports = []
@@ -7,11 +7,12 @@ status_map = {"UNKNOWN": 3, "CRITICAL": 2, "WARNING": 1, "OK": 0}
7
7
 
8
8
 
9
9
  class Host:
10
- def __init__(self, name, report_types=tuple(), reports=[]):
10
+ def __init__(self, name, report_types=tuple(), reports=[], path=None):
11
11
  self.name = name
12
12
  self.report_types = report_types
13
13
  self.reports = reports
14
14
  self.url_path = f"hosts/{self.name}/host.html"
15
+ self.path = path
15
16
 
16
17
  @property
17
18
  def description(self):
@@ -77,7 +78,8 @@ class ReportItem:
77
78
  report_type=None,
78
79
  report_id=None,
79
80
  is_only_item=False,
80
- is_history_item=False
81
+ is_history_item=False,
82
+ history=None,
81
83
  ):
82
84
  self.raw_item = raw_item
83
85
  self.command = command
@@ -90,6 +92,7 @@ class ReportItem:
90
92
  self.host = host
91
93
  self.is_only_item = is_only_item
92
94
  self.is_history_item = is_history_item
95
+ self.history = history
93
96
 
94
97
  def __repr__(self):
95
98
  return f"ReportItem(command={self.command}, status={self.status})"
@@ -113,7 +116,17 @@ class ReportItem:
113
116
 
114
117
  @property
115
118
  def url_path(self):
116
- return f"{self.report_type}_{self.filename}.html"
119
+ basename = f"{self.report_type}_{self.filename}"
120
+ if self.is_history_item:
121
+ basename = self.id
122
+ return f"{basename}.html"
123
+
124
+ @property
125
+ def raw_path(self):
126
+ basename = f"{self.report_type}_{self.filename}"
127
+ if self.is_history_item:
128
+ basename = self.id
129
+ return f"{basename}.txt"
117
130
 
118
131
  @property
119
132
  def status(self):
@@ -22,6 +22,9 @@ class Sysview:
22
22
  self.reports = reports
23
23
  self.html_root = html_root
24
24
  self.html_hosts = html_root / "hosts"
25
+ self._env = Environment(
26
+ loader=PackageLoader("sysview_py"), autoescape=select_autoescape()
27
+ )
25
28
 
26
29
  @property
27
30
  def all_services(self):
@@ -50,45 +53,55 @@ class Sysview:
50
53
  Host(name=host[0], report_types=host[1]) for host in self.db.get_hosts()
51
54
  ]
52
55
 
53
- def update_html(self):
54
- shutil.rmtree(self.html_root)
55
- self.html_root.mkdir(parents=True)
56
- self.html_hosts.mkdir(parents=True)
57
- shutil.copytree(STATIC_FILES_PATH, self.html_root / STATIC_FILES_PATH.name)
58
- env = Environment(
59
- loader=PackageLoader("sysview_py"), autoescape=select_autoescape()
56
+ def write_host_page(self, host):
57
+ html = self._env.get_template("card_view.html").render(
58
+ items=host.all_items,
59
+ title=host.name,
60
+ active="Hosts",
61
+ path="../../",
62
+ auto_refresh=True,
60
63
  )
61
- for host in self.hosts:
62
- html = env.get_template("card_view.html").render(
63
- items=host.all_items,
64
+ html_path = host.path / "host.html"
65
+ with open(html_path, "w") as f:
66
+ f.write(html)
67
+
68
+ def write_item_page(self, host, item):
69
+ html = self._env.get_template("service.html").render(
70
+ item=item,
71
+ title=host.name,
72
+ subtitle=item.name,
73
+ active="Services",
74
+ path="../../",
75
+ auto_refresh=False,
76
+ item_history=item.history
77
+ )
78
+ html_path = host.path / f"{item.report_type}_{item.filename}.html"
79
+ raw_path = host.path / f"{item.report_type}_{item.filename}.txt"
80
+ with open(html_path, "w") as f:
81
+ f.write(html)
82
+ with open(raw_path, "w") as f:
83
+ f.write(item.raw_item)
84
+
85
+ def write_item_history(self, host, item):
86
+ for history_item in item.history:
87
+ html = self._env.get_template("service.html").render(
88
+ item=history_item,
64
89
  title=host.name,
65
- active="Hosts",
90
+ subtitle=f"{history_item.name} ({history_item.date})",
91
+ active="Services",
66
92
  path="../../",
67
- auto_refresh=True,
93
+ auto_refresh=False,
94
+ item_history=item.history
68
95
  )
69
- host_dir = self.html_hosts / host.name
70
- host_dir.mkdir()
71
- html_path = host_dir / "host.html"
96
+ html_path = host.path / f"{history_item.id}.html"
97
+ raw_path = host.path / f"{history_item.id}.txt"
72
98
  with open(html_path, "w") as f:
73
99
  f.write(html)
74
- for item in host.all_items:
75
- item_history = self.db.get_item_history(item)
76
- html = env.get_template("service.html").render(
77
- item=item,
78
- title=host.name,
79
- subtitle=item.name,
80
- active="Services",
81
- path="../../",
82
- auto_refresh=False,
83
- item_history=item_history,
84
- )
85
- html_path = host_dir / f"{item.report_type}_{item.filename}.html"
86
- raw_path = host_dir / f"{item.report_type}_{item.filename}.txt"
87
- with open(html_path, "w") as f:
88
- f.write(html)
89
- with open(raw_path, "w") as f:
90
- f.write(item.raw_item)
91
- html = env.get_template("card_view.html").render(
100
+ with open(raw_path, "w") as f:
101
+ f.write(item.raw_item)
102
+
103
+ def write_services_page(self):
104
+ html = self._env.get_template("card_view.html").render(
92
105
  items=self.all_services,
93
106
  title="Services",
94
107
  active="Services",
@@ -98,13 +111,31 @@ class Sysview:
98
111
  html_path = self.html_root / f"services.html"
99
112
  with open(html_path, "w") as f:
100
113
  f.write(html)
101
- html = env.get_template("card_view.html").render(
114
+
115
+ def write_index_page(self):
116
+ html = self._env.get_template("card_view.html").render(
102
117
  items=self.hosts, title="Hosts", active="Hosts", path="", auto_refresh=True
103
118
  )
104
119
  html_path = self.html_root / "index.html"
105
120
  with open(html_path, "w") as f:
106
121
  f.write(html)
107
122
 
123
+ def update_html(self):
124
+ shutil.rmtree(self.html_root)
125
+ self.html_root.mkdir(parents=True)
126
+ self.html_hosts.mkdir(parents=True)
127
+ shutil.copytree(STATIC_FILES_PATH, self.html_root / STATIC_FILES_PATH.name)
128
+ for host in self.hosts:
129
+ host.path = self.html_hosts / host.name
130
+ host.path.mkdir()
131
+ self.write_host_page(host=host)
132
+ for item in host.all_items:
133
+ item.history = self.db.get_item_history(item)
134
+ self.write_item_history(host=host, item=item)
135
+ self.write_item_page(host=host, item=item)
136
+ self.write_services_page()
137
+ self.write_index_page()
138
+
108
139
  def get_latest_reports(self):
109
140
  for host in self.hosts:
110
141
  host.reports = self.db.get_latest_reports(hostname=host.name)
@@ -2,7 +2,7 @@
2
2
  <nav>
3
3
  <a href="host.html">Show host</a>
4
4
  |
5
- <a href="{{ item.report_type }}_{{ item.filename }}.txt">Raw report</a>
5
+ <a href="{{ item.raw_path }}">Raw report</a>
6
6
  </nav>
7
7
  </table>
8
8
  <pre id="{{item.status}}">{{ item.status }}: {{ item.status_text }}</pre>
@@ -15,10 +15,16 @@
15
15
  <h3>History</h3>
16
16
  <table>
17
17
  {% for history_item in item_history %}
18
- <tr>
19
- <td id="{{ history_item.status }}">{{ history_item.date }}</td>
18
+ <tr>
19
+ <td id="{{ history_item.status }}">
20
+ {% if history_item.id == item.id %}
21
+ <a id="{{ history_item.status }}" href="{{ history_item.url_path }}"><b>{{ history_item.date }}</b></a>
22
+ {% else %}
23
+ <a id="{{ history_item.status }}" href="{{ history_item.url_path }}">{{ history_item.date }}</a>
24
+ {% endif %}
25
+ </td>
20
26
  <td>{{ history_item.status_text }}</td>
21
- </tr>
27
+ </tr>
22
28
  {% endfor %}
23
29
  </table>
24
30
  {% endif %}
File without changes