@xeplr/monitor 0.1.3 → 1.0.0

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.
Files changed (67) hide show
  1. package/README.md +138 -125
  2. package/api/README.md +71 -0
  3. package/api/app.js +46 -0
  4. package/api/bin/auth.js +37 -0
  5. package/api/bin/www +72 -0
  6. package/api/db/setup.js +10 -0
  7. package/api/env.required.js +37 -0
  8. package/api/lib/host.js +188 -0
  9. package/api/lib/jobs.js +83 -0
  10. package/api/lib/services.js +67 -0
  11. package/api/lib/stats.js +362 -0
  12. package/{lib → api/lib}/systemd.js +75 -14
  13. package/api/migrations/0001_monitor.sql +43 -0
  14. package/api/migrations/0002_backup_connections.sql +26 -0
  15. package/api/migrations/0003_settings_audit.sql +18 -0
  16. package/api/migrations/0004_monitor_stats.sql +73 -0
  17. package/api/migrations/README.md +9 -0
  18. package/api/migrations-auth/0006_monitor.sql +56 -0
  19. package/api/migrations-auth/0007_monitor_home.sql +42 -0
  20. package/api/migrations-auth/0008_backups.sql +44 -0
  21. package/api/migrations-auth/0009_app_settings.sql +44 -0
  22. package/api/migrations-auth/0010_monitor_history.sql +22 -0
  23. package/api/models/index.js +18 -0
  24. package/api/package-lock.json +2839 -0
  25. package/api/package.json +39 -0
  26. package/api/production.env.example +61 -0
  27. package/api/routes/appSettings.js +73 -0
  28. package/api/routes/index.js +47 -0
  29. package/api/routes/monitor.js +196 -0
  30. package/api/setup.schema.js +166 -0
  31. package/deploy/control.sh +27 -0
  32. package/deploy/deploy.js +183 -0
  33. package/deploy/install.sh +177 -77
  34. package/deploy/settings.cjs +93 -0
  35. package/package.json +28 -47
  36. package/ui/.env +8 -0
  37. package/ui/README.md +29 -0
  38. package/ui/index.html +13 -0
  39. package/ui/package-lock.json +2094 -0
  40. package/ui/package.json +31 -0
  41. package/ui/pages.json +33 -0
  42. package/ui/public/logo-wide.svg +17 -0
  43. package/ui/public/logo.svg +10 -0
  44. package/ui/src/App.jsx +130 -0
  45. package/ui/src/api/monitor.js +6 -0
  46. package/ui/src/index.css +414 -0
  47. package/ui/src/main.jsx +27 -0
  48. package/ui/src/pages/Connections.jsx +9 -0
  49. package/ui/src/pages/Home.jsx +8 -0
  50. package/ui/src/pages/Monitor.jsx +9 -0
  51. package/ui/vite.config.js +56 -0
  52. package/bin/deploy.js +0 -181
  53. package/bin/server.js +0 -19
  54. package/bin/users.js +0 -86
  55. package/deploy/monitor.env.example +0 -16
  56. package/deploy/nginx.conf.example +0 -18
  57. package/deploy/sudoers.example +0 -13
  58. package/deploy/xeplr-monitor.service +0 -26
  59. package/index.js +0 -89
  60. package/lib/router.js +0 -179
  61. package/lib/services.js +0 -76
  62. package/lib/session.js +0 -78
  63. package/lib/store.js +0 -73
  64. package/lib/users.js +0 -119
  65. package/public/assets/index-BsBYpvNI.js +0 -18
  66. package/public/assets/index-DNZBwVTn.css +0 -1
  67. package/public/index.html +0 -15
@@ -0,0 +1,9 @@
1
+ # migrations
2
+
3
+ Hand-written `.sql` files, applied once each, in name order, every time the API
4
+ starts (`0001_something.sql`, `0002_…`).
5
+
6
+ **Tables for screens are not written here.** A screen's table (`tasks`) is
7
+ created and changed by **Publish** in Configure UI → Forms — see `screens/`.
8
+
9
+ Put here only tables no screen owns: lookup data, a table your own code writes.
@@ -0,0 +1,56 @@
1
+ -- 0006_monitor.sql — the Monitor page, its routes, and who may use them.
2
+ --
3
+ -- The menu name is the KEY: it must match ui/src/App.jsx's drawerItems `key`
4
+ -- EXACTLY ('Monitor'). @xeplr/ui-account drops unrecognised names silently.
5
+ --
6
+ -- The apis names must match routes/monitor.js's API map EXACTLY: each route
7
+ -- checks its own name with requireApi.
8
+ --
9
+ -- Groups, and the roles they go to:
10
+ -- monitor:view see the services, their processes and logs Super Admin, CompanyAdmin, Admin, Editor, Creator, Viewer
11
+ -- monitor:control start, stop, restart, kill Super Admin, CompanyAdmin, Admin, Editor
12
+ -- monitor:manage add, edit, remove watched services Super Admin, CompanyAdmin, Admin
13
+ -- monitor:audit read the audit log Super Admin, CompanyAdmin, Admin
14
+ -- Changed later in Admin → roles; these are the starting grants.
15
+ -- Inserts are insert-if-absent, safe to run again.
16
+
17
+ INSERT INTO "menus" (id, name, "menuGroup", "isPublic", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
18
+ SELECT encode(gen_random_bytes(12), 'hex'), 'Monitor', 'monitor:view', false, true, '*', now(), now()
19
+ WHERE NOT EXISTS (SELECT 1 FROM "menus" WHERE name = 'Monitor');
20
+
21
+ INSERT INTO "apis" (id, name, "apiGroup", "isPublic", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
22
+ SELECT encode(gen_random_bytes(12), 'hex'), name, "group", false, true, '*', now(), now()
23
+ FROM (VALUES
24
+ ('View monitor settings', 'monitor:view'),
25
+ ('List monitored services', 'monitor:view'),
26
+ ('Read service logs', 'monitor:view'),
27
+ ('Follow service logs live', 'monitor:view'),
28
+ ('Start, stop, restart or kill a service', 'monitor:control'),
29
+ ('Add a monitored service', 'monitor:manage'),
30
+ ('Edit a monitored service', 'monitor:manage'),
31
+ ('Remove a monitored service', 'monitor:manage'),
32
+ ('Read the monitor audit log', 'monitor:audit')
33
+ ) AS v(name, "group")
34
+ WHERE NOT EXISTS (SELECT 1 FROM "apis" a WHERE a."apiGroup" = v."group" AND a.name = v.name);
35
+
36
+ -- Each role → the monitor groups it gets: its apis, and the Monitor menu.
37
+ WITH grants(role, "group") AS (VALUES
38
+ ('Super Admin', 'monitor:view'), ('Super Admin', 'monitor:control'), ('Super Admin', 'monitor:manage'), ('Super Admin', 'monitor:audit'),
39
+ ('CompanyAdmin', 'monitor:view'), ('CompanyAdmin', 'monitor:control'), ('CompanyAdmin', 'monitor:manage'), ('CompanyAdmin', 'monitor:audit'),
40
+ ('Admin', 'monitor:view'), ('Admin', 'monitor:control'), ('Admin', 'monitor:manage'), ('Admin', 'monitor:audit'),
41
+ ('Editor', 'monitor:view'), ('Editor', 'monitor:control'),
42
+ ('Creator', 'monitor:view'),
43
+ ('Viewer', 'monitor:view')
44
+ )
45
+ INSERT INTO "apisRolesMapping" (id, "roleId", "apiId", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
46
+ SELECT encode(gen_random_bytes(12), 'hex'), r.id, a.id, true, '*', now(), now()
47
+ FROM grants g
48
+ JOIN "roles" r ON r.name = g.role
49
+ JOIN "apis" a ON a."apiGroup" = g."group"
50
+ WHERE NOT EXISTS (SELECT 1 FROM "apisRolesMapping" m WHERE m."roleId" = r.id AND m."apiId" = a.id);
51
+
52
+ INSERT INTO "menuRolesMapping" (id, "menuId", "roleId", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
53
+ SELECT encode(gen_random_bytes(12), 'hex'), m.id, r.id, true, '*', now(), now()
54
+ FROM "roles" r JOIN "menus" m ON m.name = 'Monitor'
55
+ WHERE r.name IN ('Super Admin', 'CompanyAdmin', 'Admin', 'Editor', 'Creator', 'Viewer')
56
+ AND NOT EXISTS (SELECT 1 FROM "menuRolesMapping" x WHERE x."roleId" = r.id AND x."menuId" = m.id);
@@ -0,0 +1,42 @@
1
+ -- 0007_monitor_home.sql — the Home page: the server itself (memory, CPU,
2
+ -- disks, uptime), and its one route.
3
+ --
4
+ -- The menu name is the KEY: it must match ui/src/App.jsx's drawerItems `key`
5
+ -- EXACTLY ('Home'). The apis name must match routes/monitor.js's API map.
6
+ -- Granted with monitor:view: everyone who may see the services may see the
7
+ -- server they run on. Inserts are insert-if-absent, safe to run again.
8
+
9
+ INSERT INTO "menus" (id, name, "menuGroup", "isPublic", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
10
+ SELECT encode(gen_random_bytes(12), 'hex'), 'Home', 'monitor:view', false, true, '*', now(), now()
11
+ WHERE NOT EXISTS (SELECT 1 FROM "menus" WHERE name = 'Home');
12
+ -- A database first made by @xeplr/cli has a public 'Home' row of its own; it is
13
+ -- this page now.
14
+ UPDATE "menus" SET "menuGroup" = 'monitor:view', "isPublic" = false WHERE name = 'Home';
15
+
16
+ INSERT INTO "apis" (id, name, "apiGroup", "isPublic", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
17
+ SELECT encode(gen_random_bytes(12), 'hex'), name, "group", false, true, '*', now(), now()
18
+ FROM (VALUES
19
+ ('View server statistics', 'monitor:view')
20
+ ) AS v(name, "group")
21
+ WHERE NOT EXISTS (SELECT 1 FROM "apis" a WHERE a."apiGroup" = v."group" AND a.name = v.name);
22
+
23
+ WITH grants(role, "group") AS (VALUES
24
+ ('Super Admin', 'monitor:view'),
25
+ ('CompanyAdmin', 'monitor:view'),
26
+ ('Admin', 'monitor:view'),
27
+ ('Editor', 'monitor:view'),
28
+ ('Creator', 'monitor:view'),
29
+ ('Viewer', 'monitor:view')
30
+ )
31
+ INSERT INTO "apisRolesMapping" (id, "roleId", "apiId", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
32
+ SELECT encode(gen_random_bytes(12), 'hex'), r.id, a.id, true, '*', now(), now()
33
+ FROM grants g
34
+ JOIN "roles" r ON r.name = g.role
35
+ JOIN "apis" a ON a."apiGroup" = g."group" AND a.name = 'View server statistics'
36
+ WHERE NOT EXISTS (SELECT 1 FROM "apisRolesMapping" m WHERE m."roleId" = r.id AND m."apiId" = a.id);
37
+
38
+ INSERT INTO "menuRolesMapping" (id, "menuId", "roleId", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
39
+ SELECT encode(gen_random_bytes(12), 'hex'), m.id, r.id, true, '*', now(), now()
40
+ FROM "roles" r JOIN "menus" m ON m.name = 'Home'
41
+ WHERE r.name IN ('Super Admin', 'CompanyAdmin', 'Admin', 'Editor', 'Creator', 'Viewer')
42
+ AND NOT EXISTS (SELECT 1 FROM "menuRolesMapping" x WHERE x."roleId" = r.id AND x."menuId" = m.id);
@@ -0,0 +1,44 @@
1
+ -- 0008_backups.sql — database backups: the Backup connections page and its
2
+ -- routes (routes/connections.js). The jobs themselves are @xeplr/jobs', with
3
+ -- its own rows (node_modules/@xeplr/jobs/migrations-auth, also run: see
4
+ -- XEPLR_AUTH_MIGRATIONS).
5
+ --
6
+ -- The menu name is the KEY: it must match ui/src/App.jsx's drawerItems `key`
7
+ -- EXACTLY ('Backup connections'). The apis names must match
8
+ -- routes/connections.js's API map. Only admins: these are the logins to every
9
+ -- database on the server. Inserts are insert-if-absent, safe to run again.
10
+
11
+ INSERT INTO "menus" (id, name, "menuGroup", "isPublic", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
12
+ SELECT encode(gen_random_bytes(12), 'hex'), 'Backup connections', 'backups:view', false, true, '*', now(), now()
13
+ WHERE NOT EXISTS (SELECT 1 FROM "menus" WHERE name = 'Backup connections');
14
+
15
+ INSERT INTO "apis" (id, name, "apiGroup", "isPublic", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
16
+ SELECT encode(gen_random_bytes(12), 'hex'), name, "group", false, true, '*', now(), now()
17
+ FROM (VALUES
18
+ ('List backup connections', 'backups:view'),
19
+ ('Test a backup connection', 'backups:view'),
20
+ ('Save a backup connection', 'backups:manage'),
21
+ ('Remove a backup connection', 'backups:manage')
22
+ ) AS v(name, "group")
23
+ WHERE NOT EXISTS (SELECT 1 FROM "apis" a WHERE a."apiGroup" = v."group" AND a.name = v.name);
24
+
25
+ WITH grants(role, "group") AS (VALUES
26
+ ('Super Admin', 'backups:view'),
27
+ ('CompanyAdmin', 'backups:view'),
28
+ ('Admin', 'backups:view'),
29
+ ('Super Admin', 'backups:manage'),
30
+ ('CompanyAdmin', 'backups:manage'),
31
+ ('Admin', 'backups:manage')
32
+ )
33
+ INSERT INTO "apisRolesMapping" (id, "roleId", "apiId", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
34
+ SELECT encode(gen_random_bytes(12), 'hex'), r.id, a.id, true, '*', now(), now()
35
+ FROM grants g
36
+ JOIN "roles" r ON r.name = g.role
37
+ JOIN "apis" a ON a."apiGroup" = g."group"
38
+ WHERE NOT EXISTS (SELECT 1 FROM "apisRolesMapping" m WHERE m."roleId" = r.id AND m."apiId" = a.id);
39
+
40
+ INSERT INTO "menuRolesMapping" (id, "menuId", "roleId", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
41
+ SELECT encode(gen_random_bytes(12), 'hex'), m.id, r.id, true, '*', now(), now()
42
+ FROM "roles" r JOIN "menus" m ON m.name = 'Backup connections'
43
+ WHERE r.name IN ('Super Admin', 'CompanyAdmin', 'Admin')
44
+ AND NOT EXISTS (SELECT 1 FROM "menuRolesMapping" x WHERE x."roleId" = r.id AND x."menuId" = m.id);
@@ -0,0 +1,44 @@
1
+ -- 0009_app_settings.sql
2
+ -- THE APP'S OWN SETTINGS (settings menu → App settings): the install's env
3
+ -- file seen and changed after setup, then the app restarts into it
4
+ -- (routes/appSettings.js over @xeplr/setup settingsRouter; the page is
5
+ -- @xeplr/ui-setup's, mounted in ui/src/App.jsx).
6
+ --
7
+ -- SUPER ADMIN ONLY, deliberately: it changes the whole install (database
8
+ -- login, mail, data folder) for every company on it, so a company admin,
9
+ -- whose rights end at their own company, must never reach it. Marked scope
10
+ -- 'system' — the one kind of API that may be mapped to Super Admin alone.
11
+ --
12
+ -- settings:app — Super Admin only (system)
13
+ -- View app settings GET /api/app-settings
14
+ -- Check app settings POST /api/app-settings/check
15
+ -- Save app settings POST /api/app-settings
16
+ --
17
+ -- Insert-if-absent throughout: safe to run again.
18
+
19
+ INSERT INTO "apis" (id, name, "apiGroup", "scope", "scopeLocked", "isPublic", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
20
+ SELECT encode(gen_random_bytes(12), 'hex'), v.name, v."group", 'system', true, false, true, '*', now(), now()
21
+ FROM (VALUES
22
+ ('View app settings', 'settings:app'),
23
+ ('Check app settings', 'settings:app'),
24
+ ('Save app settings', 'settings:app')
25
+ ) AS v(name, "group")
26
+ WHERE NOT EXISTS (SELECT 1 FROM "apis" a WHERE a.name = v.name AND a."apiGroup" = v."group");
27
+
28
+ INSERT INTO "apisRolesMapping" (id, "roleId", "apiId", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
29
+ SELECT encode(gen_random_bytes(12), 'hex'), r.id, a.id, true, '*', now(), now()
30
+ FROM "roles" r CROSS JOIN "apis" a
31
+ WHERE a."apiGroup" = 'settings:app' AND r.name = 'Super Admin'
32
+ AND NOT EXISTS (SELECT 1 FROM "apisRolesMapping" m WHERE m."roleId" = r.id AND m."apiId" = a.id);
33
+
34
+ -- The page, in the settings menu. The name is the KEY: it must match
35
+ -- ui/src/App.jsx settingsOverrides `key` EXACTLY ('App settings').
36
+ INSERT INTO "menus" (id, name, "menuGroup", "isPublic", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
37
+ SELECT encode(gen_random_bytes(12), 'hex'), 'App settings', 'settings:app', false, true, '*', now(), now()
38
+ WHERE NOT EXISTS (SELECT 1 FROM "menus" WHERE name = 'App settings');
39
+
40
+ INSERT INTO "menuRolesMapping" (id, "roleId", "menuId", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
41
+ SELECT encode(gen_random_bytes(12), 'hex'), r.id, m.id, true, '*', now(), now()
42
+ FROM "roles" r CROSS JOIN "menus" m
43
+ WHERE r.name = 'Super Admin' AND m.name = 'App settings'
44
+ AND NOT EXISTS (SELECT 1 FROM "menuRolesMapping" x WHERE x."roleId" = r.id AND x."menuId" = m.id);
@@ -0,0 +1,22 @@
1
+ -- 0010_monitor_history.sql — the history behind the charts (routes/monitor.js):
2
+ -- a service's memory, CPU, spikes, log counts and requests over time; the
3
+ -- server's; and requests by address. Read-only, so granted with monitor:view
4
+ -- to everyone who may see the services. The names must match routes/monitor.js's
5
+ -- API map. Inserts are insert-if-absent, safe to run again.
6
+
7
+ INSERT INTO "apis" (id, name, "apiGroup", "isPublic", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
8
+ SELECT encode(gen_random_bytes(12), 'hex'), name, "group", false, true, '*', now(), now()
9
+ FROM (VALUES
10
+ ('View service history', 'monitor:view'),
11
+ ('View server history', 'monitor:view'),
12
+ ('View service requests', 'monitor:view')
13
+ ) AS v(name, "group")
14
+ WHERE NOT EXISTS (SELECT 1 FROM "apis" a WHERE a."apiGroup" = v."group" AND a.name = v.name);
15
+
16
+ WITH grants(role) AS (VALUES ('Super Admin'), ('CompanyAdmin'), ('Admin'), ('Editor'), ('Creator'), ('Viewer'))
17
+ INSERT INTO "apisRolesMapping" (id, "roleId", "apiId", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
18
+ SELECT encode(gen_random_bytes(12), 'hex'), r.id, a.id, true, '*', now(), now()
19
+ FROM grants g
20
+ JOIN "roles" r ON r.name = g.role
21
+ JOIN "apis" a ON a."apiGroup" = 'monitor:view' AND a.name IN ('View service history', 'View server history', 'View service requests')
22
+ WHERE NOT EXISTS (SELECT 1 FROM "apisRolesMapping" m WHERE m."roleId" = r.id AND m."apiId" = a.id);
@@ -0,0 +1,18 @@
1
+ // The monitor's tables (migrations/0001_monitor.sql), each a class extending
2
+ // @xeplr/db's BaseModel, used by lib/services.js.
3
+ var { BaseModel } = require('@xeplr/db');
4
+
5
+ // The systemd services the monitor watches (migrations/0001_monitor.sql).
6
+ class MonitoredService extends BaseModel {
7
+ static get tableName() { return 'monitor_services'; }
8
+ static get jsonSchema() { return BaseModel.schema({ required: ['unit', 'label'] }); }
9
+ }
10
+
11
+ // Every change made through the monitor, refused ones too.
12
+ class MonitorAudit extends BaseModel {
13
+ static get tableName() { return 'monitor_audit'; }
14
+ static get jsonSchema() { return BaseModel.schema({ required: ['action', 'ok'] }); }
15
+ }
16
+
17
+
18
+ module.exports = { MonitoredService: MonitoredService, MonitorAudit: MonitorAudit };