Skip to content

Commit

Permalink
fix(reboot): fixed reboot notifier
Browse files Browse the repository at this point in the history
closes #6
  • Loading branch information
christopherpickering committed Aug 21, 2023
1 parent 328e969 commit 8570074
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 26 deletions.
3 changes: 1 addition & 2 deletions app/components/charts/databaseMemoryChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const MemoryChart = ({ url }: { url: string }) => {
if (!chart) {
return;
}
console.log(usageFetcher.data?.database);

const chartData = {
datasets: [
{
Expand Down Expand Up @@ -192,7 +192,6 @@ export const MemoryChart = ({ url }: { url: string }) => {
],
};
setOptions(getOptions());
console.log(chartData);
setChartData(chartData);
}, [usageFetcher.data]);

Expand Down
4 changes: 3 additions & 1 deletion app/components/logTable/columns.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ColumnDef } from '@tanstack/react-table';
import { Activity, AlertTriangle } from 'lucide-react';
import { Activity, AlertCircle, AlertTriangle } from 'lucide-react';
import { DataTableColumnHeader } from '~/components/table/data-table-column-header';
import { format } from 'date-fns';
import { Link } from '@remix-run/react';
Expand All @@ -20,6 +20,8 @@ export const columns: ColumnDef<any>[] = [
<div className="">
{row.getValue('type') == 'error' ? (
<AlertTriangle className="text-red-500" size={14} />
) : row.getValue('type') == 'warning' ? (
<AlertCircle className="text-orange-600" size={14} />
) : (
<Activity className="text-emerald-600" size={14} />
)}
Expand Down
1 change: 1 addition & 0 deletions app/models/monitor.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export function getMonitor({ id }: Pick<Monitor, 'id'>) {
httpDomain: true,
httpWorkstation: true,
sqlConnectionString: true,
lastBootTime: true,
drives: {
select: {
id: true,
Expand Down
7 changes: 4 additions & 3 deletions app/monitors/ubuntu.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Monitor,
getMonitor,
getMonitorDisabledDrives,
monitorError,
setDriveDays,
Expand Down Expand Up @@ -86,7 +87,7 @@ export default async function UbuntuMonitor({ monitor }: { monitor: Monitor }) {
let lastBootTime = null;

try {
lastBootTime = new Date(lastBoot).toString();
lastBootTime = new Date(lastBoot).toISOString();
} catch (e) {}

/*
Expand Down Expand Up @@ -124,7 +125,7 @@ export default async function UbuntuMonitor({ monitor }: { monitor: Monitor }) {
).length == 0;
return l;
});

const oldMonitor = await getMonitor({ id: monitor.id });
const data = await updateMonitor({
id: monitor.id,
data: {
Expand Down Expand Up @@ -207,7 +208,7 @@ export default async function UbuntuMonitor({ monitor }: { monitor: Monitor }) {

disposeSsh(ssh);

await Notifier({ job: monitor.id });
await Notifier({ job: monitor.id, oldMonitor });

console.log(`successfully ran ${monitor.type} monitor: ${monitor.id}`);
} catch (e) {
Expand Down
8 changes: 5 additions & 3 deletions app/monitors/windows.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { decrypt } from '@/lib/utils';
import { NodeSSH } from 'node-ssh';
import {
Monitor,
getMonitor,
getMonitorDisabledDrives,
monitorError,
setDriveDays,
Expand Down Expand Up @@ -214,6 +215,7 @@ export default async function WindowsMonitor({
return l;
});

const oldMonitor = await getMonitor({ id: monitor.id });
const data = await updateMonitor({
id: monitor.id,
data: {
Expand All @@ -224,7 +226,7 @@ export default async function WindowsMonitor({
model: cs.Model,
os: os.Caption,
osVersion: os.Version,
lastBootTime: lastBoot,
lastBootTime: lastBoot.toISOString(),
cpuManufacturer: pc.Manufacturer,
cpuModel: pc.Caption,
cpuCores: pc.NumberOfCores.toString(),
Expand All @@ -236,7 +238,7 @@ export default async function WindowsMonitor({
memoryFree: (os.FreePhysicalMemory * 1000).toString(),
memoryTotal: (os.TotalVisibleMemorySize * 1000).toString(),

cpuLoad: pc.LoadPercentage.toString(),
cpuLoad: pc.LoadPercentage ? pc.LoadPercentage.toString() : null,
cpuSpeed: pc.CurrentClockSpeed.toString(),
},
drives: updateableDrives.map(
Expand Down Expand Up @@ -293,7 +295,7 @@ export default async function WindowsMonitor({

disposeSsh(ssh);

await Notifier({ job: monitor.id });
await Notifier({ job: monitor.id, oldMonitor });

console.log(`successfully ran ${monitor.type} monitor: ${monitor.id}`);
} catch (e) {
Expand Down
21 changes: 8 additions & 13 deletions app/notifications/checks/monitors/reboot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,21 @@ import { sendNotification } from '~/notifications/notifier';

export default async function rebootNotifier({
monitor,
oldMonitor,
}: {
monitor: Monitor;
oldMonitor: Monitor;
}) {
console.log('reboot notifier!');
// don't notify if disabled.
if (!monitor.rebootNotify) return;

// get last reboot time
const lastBootTime = await getMonitorBootTime({ id: monitor.id });
console.log(monitor.lastBootTime, oldMonitor.lastBootTime);

// if new , update time and skip notification
if (!monitor.rebootNotifySentAt) {
return setMonitorRebootSentAt({
id: monitor.id,
rebootNotifySentAt: new Date(),
});
}

if (monitor.lastBootTime != lastBootTime) {
const subject = `[${monitor.host}] Rebooted.`;
const message = `[${monitor.host}] Rebooted.`;
// send notification if it has changed
if (monitor.lastBootTime != oldMonitor.lastBootTime) {
const subject = `⏰ [${monitor.host}] Reboot time changed.`;
const message = `[${monitor.host}] Reboot time changed.`;

monitor.rebootNotifyTypes.map(async (notification: Notification) => {
try {
Expand Down
6 changes: 4 additions & 2 deletions app/notifications/notifier.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ import collectionNotifier from './checks/monitors/collection';
export default async function Notifier({
job,
message,
oldMonitor,
}: {
job: string;
message?: string;
oldMonitor?: Monitor;
}) {
const monitor = await getMonitor({ id: job });

return collectionNotifier({ monitor, message });
await collectionNotifier({ monitor, message });

if (monitor.type === 'windows' || monitor.type === 'ubuntu') {
// reboot notifier
await rebootNotifier({ monitor });
await rebootNotifier({ monitor, oldMonitor });

// drive notifications
monitor?.drives?.map(async (drive: Drive & { usage: DriveUsage[] }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export const FilesTable = ({
const [rowSelection, setRowSelection] = React.useState({});
const [columnVisibility, setColumnVisibility] =
React.useState<VisibilityState>({
title: false,
recoveryModel: false,
compatLevel: false,
backupLogDate: false,
Expand All @@ -45,7 +44,7 @@ export const FilesTable = ({
);

const [sorting, setSorting] = React.useState<SortingState>([
{ id: 'name', desc: true },
{ id: 'fileName', desc: true },
]);

const table = useReactTable({
Expand Down

0 comments on commit 8570074

Please sign in to comment.