diff --git a/.gitignore b/.gitignore index 8dadcd1..5ba5218 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ pti.code-workspace temporary-captain-to-deploy.tar deploy.sh -output.json \ No newline at end of file +output.json +client_sample.json diff --git a/server/prisma/migrations/20220602170840_wifi_stats/migration.sql b/server/prisma/migrations/20220602170840_wifi_stats/migration.sql new file mode 100644 index 0000000..5e549da --- /dev/null +++ b/server/prisma/migrations/20220602170840_wifi_stats/migration.sql @@ -0,0 +1,59 @@ +/* + Warnings: + + - You are about to drop the `Statistic` table. If the table is not empty, all the data it contains will be lost. + +*/ +-- AlterTable +ALTER TABLE "AccessPoint" ADD COLUMN "inventoryTag" TEXT, +ADD COLUMN "sshPassword" TEXT, +ADD COLUMN "sshUser" TEXT; + +-- AlterTable +ALTER TABLE "WifiDevice" ADD COLUMN "frequency" TEXT, +ADD COLUMN "identity" TEXT, +ADD COLUMN "ownerId" INTEGER, +ADD COLUMN "protocol" TEXT, +ADD COLUMN "signalStrength" INTEGER, +ADD COLUMN "speed" INTEGER, +ADD COLUMN "usage" INTEGER; + +-- DropTable +DROP TABLE "Statistic"; + +-- CreateTable +CREATE TABLE "WifiStats" ( + "id" SERIAL NOT NULL, + "timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "clients" INTEGER NOT NULL, + "accessPointId" INTEGER NOT NULL, + + CONSTRAINT "WifiStats_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Network" ( + "id" SERIAL NOT NULL, + "name" TEXT NOT NULL, + "shortName" TEXT NOT NULL, + "cidr" TEXT NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "Network_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "Network_name_key" ON "Network"("name"); + +-- CreateIndex +CREATE UNIQUE INDEX "Network_shortName_key" ON "Network"("shortName"); + +-- CreateIndex +CREATE UNIQUE INDEX "Network_cidr_key" ON "Network"("cidr"); + +-- AddForeignKey +ALTER TABLE "WifiDevice" ADD CONSTRAINT "WifiDevice_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "WifiStats" ADD CONSTRAINT "WifiStats_accessPointId_fkey" FOREIGN KEY ("accessPointId") REFERENCES "AccessPoint"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/server/prisma/schema.prisma b/server/prisma/schema.prisma index 08d85db..db38cbd 100644 --- a/server/prisma/schema.prisma +++ b/server/prisma/schema.prisma @@ -14,20 +14,22 @@ model ResetToken { usedAt DateTime? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt - userId Int - creatorId Int - creator User @relation("resettoken_to_creator", fields: [creatorId], references: [id]) - user User @relation("resettoken_to_user", fields: [userId], references: [id]) + + userId Int + user User @relation("resettoken_to_user", fields: [userId], references: [id]) + + creatorId Int + creator User @relation("resettoken_to_creator", fields: [creatorId], references: [id]) } model User { - id Int @id @default(autoincrement()) + id Int @id @default(autoincrement()) lastLogin DateTime? lastLoginPrior DateTime? roles Json? groups Json? - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt accountExpires String? badPasswordTime String? badPwdCount String? @@ -57,7 +59,7 @@ model User { objectSid String? primaryGroupID String? pwdLastSet DateTime? - sAMAccountName String @unique + sAMAccountName String @unique sAMAccountType String? sn String? thumbnailPhoto String? @@ -66,31 +68,48 @@ model User { userPrincipalName String? whenChanged String? whenCreated String? - createdTokens ResetToken[] @relation("resettoken_to_creator") - tokens ResetToken[] @relation("resettoken_to_user") - wifiDevices WifiDevice[] @relation("wifidevice_to_user") - PAHost PAHost[] @relation("pahost_to_user") + + createdTokens ResetToken[] @relation("resettoken_to_creator") + tokens ResetToken[] @relation("resettoken_to_user") + wifiDevices WifiDevice[] @relation("wifidevice_to_user") + ownedWifiDevices WifiDevice[] @relation("wifidevice_to_owner") + + PAHost PAHost[] @relation("pahost_to_user") } model WifiDevice { - id Int @id @default(autoincrement()) - oui String? - mac String @unique - hostname String? - firstSeen DateTime? @default(now()) - lastSeen DateTime? - essid String? - ip String? - uptime String? - apName String? + id Int @id @default(autoincrement()) + oui String? + mac String @unique + hostname String? + firstSeen DateTime? @default(now()) + lastSeen DateTime? + ip String? + + essid String? + uptime String? + apName String? + signalStrength Int? + frequency String? + protocol String? + speed Int? + usage Int? + + status Status? + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + controller String @default("unknown") + + identity String? // The user informed by the controller + accessPointId Int? accessPoint AccessPoint? @relation("wifidevice_to_ap", fields: [accessPointId], references: [id]) - status Status? - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt - userId Int? - controller String @default("unknown") - user User? @relation("wifidevice_to_user", fields: [userId], references: [id]) + + userId Int? // The connected User + user User? @relation("wifidevice_to_user", fields: [userId], references: [id]) + + ownerId Int? // The owner of the device + owner User? @relation("wifidevice_to_owner", fields: [ownerId], references: [id]) } enum Status { @@ -116,16 +135,6 @@ enum LogLevel { ERROR } -model Statistic { - id Int @id @default(autoincrement()) - timestamp DateTime @default(now()) - onlineUsers Int - offlineUsers Int - totalUsers Int - totalWifiDevices Int - onlineWifiDevices Int -} - model PAHost { id Int @id @default(autoincrement()) user String @@ -141,20 +150,44 @@ model PAHost { } model AccessPoint { - id Int @id @default(autoincrement()) - mac String @unique - hostname String @unique - name String? - local String? - notes String? + id Int @id @default(autoincrement()) + mac String @unique + hostname String @unique + name String? + local String? + notes String? + inventoryTag String? + uptime String? controller String? model String? ip String? clients Int? + sshUser String? + sshPassword String? + createdAt DateTime @default(now()) updatedAt DateTime @updatedAt wifiDevices WifiDevice[] @relation("wifidevice_to_ap") + WifiStats WifiStats[] @relation("wifistats_to_ap") +} + +model WifiStats { + id Int @id @default(autoincrement()) + timestamp DateTime @default(now()) + clients Int + accessPointId Int + accessPoint AccessPoint? @relation("wifistats_to_ap", fields: [accessPointId], references: [id]) +} + +model Network { + id Int @id @default(autoincrement()) + name String @unique + shortName String @unique + cidr String @unique + + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt } diff --git a/server/src/lib/ciscoController.js b/server/src/lib/ciscoController.js index c9b86b3..a49bc75 100644 --- a/server/src/lib/ciscoController.js +++ b/server/src/lib/ciscoController.js @@ -105,7 +105,12 @@ export async function getOnlineWifiDevices() { uptime: client.UT.toString(), apName: client.AP, status: client.ST == 'Online' ? 'ONLINE' : 'OFFLINE', - controller: 'Cisco' + controller: 'Cisco', + signalStrength: client.SS, + frequency: client.FB, + protocol: client.PT, + speed: client.SD, + usage: client.bytes_total })) return restructuredOnlineDevices diff --git a/server/src/lib/unifiController.js b/server/src/lib/unifiController.js index 9832503..a181f9d 100644 --- a/server/src/lib/unifiController.js +++ b/server/src/lib/unifiController.js @@ -189,7 +189,12 @@ export async function getOnlineWifiDevices() { uptime: client.uptime.toString(), apName: accessPoints[0].find(ap => ap.mac === client.ap_mac).name, status: 'ONLINE', - controller: 'UniFi' + controller: 'UniFi', + signalStrength: client.signal, + frequency: null, + protocol: null, + speed: client.tx_rate, + usage: +client.tx_bytes + +client.rx_bytes })) return restructuredOnlineDevices