Skip to content

Commit

Permalink
Livestats, print counters
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed Jul 5, 2024
1 parent 0a5aace commit b67430e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
42 changes: 40 additions & 2 deletions Protest/Front/deviceview.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class DeviceView extends View {
];

static regexIPv4 = /^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/gm;
static printerTypes = ["fax", "multiprinter", "ticket printer", "printer"];

constructor(args) {
super();
Expand Down Expand Up @@ -1094,7 +1095,7 @@ class DeviceView extends View {
host = this.link.hostname.v.split(";")[0];
}

let [pingArray, cpuArray, memoryArray, diskCapacityArray, diskUsageArray] = await Promise.all([
let [pingArray, cpuArray, memoryArray, diskCapacityArray, diskUsageArray, printCounterArray] = await Promise.all([
(async ()=> {
const response = await fetch(`lifeline/ping/view?host=${host}`);
const buffer = await response.arrayBuffer();
Expand Down Expand Up @@ -1123,6 +1124,17 @@ class DeviceView extends View {
const response = await fetch(`lifeline/diskusage/view?file=${this.args.file}`);
const buffer = await response.arrayBuffer();
return new Uint8Array(buffer);
})(),

(async ()=> {
if (this.link.type && DeviceView.printerTypes.includes(this.link.type.v.toLowerCase())) {
const response = await fetch(`lifeline/printcount/view?file=${this.args.file}`);
const buffer = await response.arrayBuffer();
return new Uint8Array(buffer);
}
else {
return [];
}
})()
]);

Expand All @@ -1143,7 +1155,7 @@ class DeviceView extends View {

oMonth = (oMonth+1).toString().padStart(2,0);

const [oldPingArray, oldCpuArray, oldMemoryArray, oldDiskCapacityArray, oldDiskUsageArray] = await Promise.all([
const [oldPingArray, oldCpuArray, oldMemoryArray, oldDiskCapacityArray, oldDiskUsageArray, oldPrintCounterArray] = await Promise.all([
(async ()=> {
const response = await fetch(`lifeline/ping/view?host=${host}&date=${oYear}${oMonth}`);
const buffer = await response.arrayBuffer();
Expand Down Expand Up @@ -1172,6 +1184,17 @@ class DeviceView extends View {
const response = await fetch(`lifeline/diskusage/view?file=${this.args.file}&date=${oYear}${oMonth}`);
const buffer = await response.arrayBuffer();
return new Uint8Array(buffer);
})(),

(async ()=> {
if (this.link.type && DeviceView.printerTypes.includes(this.link.type.v.toLowerCase())) {
const response = await fetch(`lifeline/printcount/view?file=${this.args.file}&date=${oYear}${oMonth}`);
const buffer = await response.arrayBuffer();
return new Uint8Array(buffer);
}
else {
return [];
}
})()
]);

Expand All @@ -1180,6 +1203,7 @@ class DeviceView extends View {
if (oldMemoryArray.length > 0) memoryArray = [...oldMemoryArray, ...memoryArray];
if (oldDiskCapacityArray.length > 0) diskCapacityArray = [...oldDiskCapacityArray, ...diskCapacityArray];
if (oldDiskUsageArray.length > 0) diskUsageArray = [...oldDiskUsageArray, ...diskUsageArray];
if (oldPrintCounterArray.length > 0) printCounterArray = [...oldPrintCounterArray, ...printCounterArray];
}

const GenerateTimeline = ()=> {
Expand Down Expand Up @@ -1506,6 +1530,20 @@ class DeviceView extends View {

GenerateGraph(data, "Disk usage", "percent", "mono/hdd.svg");
}

if (printCounterArray.length > 0) {
let data = [];
for (let i=0; i<diskUsageArray.length-8; i+=12) {
const dateBuffer = new Uint8Array(diskUsageArray.slice(i, i+8)).buffer;
const date = Number(new DataView(dateBuffer).getBigInt64(0, true));

const counterBuffer = new Uint8Array(diskUsageArray.slice(i, i+8)).buffer;
const counter = Number(new DataView(counterBuffer).getUint32(0, true));
data.push({d:date, v:counter});
}

GenerateGraph(data, "Print counter", "vol", "mono/print.svg");
}
}

RttToColor(rtt) {
Expand Down
22 changes: 11 additions & 11 deletions Protest/Tasks/Fetch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ public static ConcurrentDictionary<string, string[]> SingleDevice(string target,

Dictionary<string, string> wmi = new Dictionary<string, string>();
Dictionary<string, string> ad = new Dictionary<string, string>();
string netbios = Protocols.NetBios.GetBiosName(ipList.First()?.ToString());
string portscan = string.Empty;
string netBios = Protocols.NetBios.GetBiosName(ipList.First()?.ToString());
string portScan = string.Empty;

Thread tWmi = null, tAd = null, tPortScan = null;

Expand Down Expand Up @@ -223,12 +223,12 @@ public static ConcurrentDictionary<string, string[]> SingleDevice(string target,

for (int i = 0; i < portsPool.Length; i++) {
if (ports[i]) {
portscan += $"{portsPool[i]}; ";
portScan += $"{portsPool[i]}; ";
}
}

if (portscan.EndsWith("; ")) {
portscan = portscan[..^2];
if (portScan.EndsWith("; ")) {
portScan = portScan[..^2];
}
});
}
Expand Down Expand Up @@ -280,8 +280,8 @@ public static ConcurrentDictionary<string, string[]> SingleDevice(string target,
}
}

if (portscan.Length > 0) {
data.TryAdd("ports", new string[] { portscan, "Port-scan", string.Empty });
if (portScan.Length > 0) {
data.TryAdd("ports", new string[] { portScan, "Port-scan", string.Empty });
}

if (wmi.TryGetValue("mac address", out string mac)) {
Expand All @@ -305,8 +305,8 @@ public static ConcurrentDictionary<string, string[]> SingleDevice(string target,
}

if (!wmi.ContainsKey("hostname")) {
if (netbios is not null && netbios.Length > 0) { //use netbios
data.TryAdd("hostname", new string[] { netbios, "NetBIOS", string.Empty });
if (netBios is not null && netBios.Length > 0) { //use netbios
data.TryAdd("hostname", new string[] { netBios, "NetBIOS", string.Empty });
}
else if (useDns && hostname is not null && hostname.Length > 0) { //use dns
data.TryAdd("hostname", new string[] { hostname, "DNS", string.Empty });
Expand Down Expand Up @@ -356,8 +356,8 @@ public static ConcurrentDictionary<string, string[]> SingleDevice(string target,
}

//if no type found, try to guess from ports
if (!data.ContainsKey("type") && portscan.Length > 0) {
int[] ports = portscan.Split(';').Select(o => int.Parse(o.Trim())).ToArray();
if (!data.ContainsKey("type") && portScan.Length > 0) {
int[] ports = portScan.Split(';').Select(o => int.Parse(o.Trim())).ToArray();

if (ports.Contains(445) && ports.Contains(3389) && (ports.Contains(53) || ports.Contains(67) || ports.Contains(389) || ports.Contains(636) || ports.Contains(853))) { //SMB, RDP, DNS, DHCP, LDAP
data.TryAdd("type", new string[] { "Server", "Port-scan", string.Empty });
Expand Down
10 changes: 4 additions & 6 deletions Protest/Tasks/Lifeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ internal static partial class Lifeline {

public static TaskWrapper task;

public static void Initialize() {
StartTask("system");
}
public static void Initialize() => StartTask("system");

public static bool StartTask(string origin) {
if (task is not null) return false;
Expand Down Expand Up @@ -413,7 +411,7 @@ private static void SnmpPrinterQuery(string file, string host, string _profile,
}

uint blackCounter = 0;
if (printCounters.TryGetValue("1.3.6.1.2.1.43.10.2.1.4.1.1", out string blackCountString)) {
if (printCounters.TryGetValue(Protocols.Snmp.Oid.PRINTER_MARKER_COUNTER_LIFE, out string blackCountString)) {
uint.TryParse(blackCountString, out blackCounter);
}

Expand All @@ -433,8 +431,8 @@ private static void SnmpPrinterQuery(string file, string host, string _profile,
using BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8, false);
writer.Write(((DateTimeOffset)now).ToUnixTimeMilliseconds()); //8 bytes
writer.Write(blackCounter); //4 bytes
writer.Write((uint)0); //4 bytes, color placeholder
writer.Write((uint)0); //4 bytes, dublex placeholder
//writer.Write((uint)0); //4 bytes, color placeholder
//writer.Write((uint)0); //4 bytes, duplex placeholder
}
}
catch { }
Expand Down

0 comments on commit b67430e

Please sign in to comment.