refactor service checks

This commit is contained in:
wh1te909
2020-05-30 03:55:31 +00:00
parent 15176fa364
commit bb30ec8ded
5 changed files with 231 additions and 300 deletions

View File

@@ -29,7 +29,7 @@
</q-item-section>
<q-item-section>Memory Check</q-item-section>
</q-item>
<q-item clickable v-close-popup @click="showAddWinSvcCheck = true">
<q-item clickable v-close-popup @click="showCheck('add', 'winsvc')">
<q-item-section side>
<q-icon size="xs" name="fas fa-cogs" />
</q-item-section>
@@ -200,17 +200,15 @@
:checkpk="checkpk"
/>
</q-dialog>
<!-- refactor below -->
<q-dialog v-model="showAddWinSvcCheck">
<AddWinSvcCheck @close="showAddWinSvcCheck = false" :agentpk="selectedAgentPk" />
</q-dialog>
<q-dialog v-model="showEditWinSvcCheck">
<EditWinSvcCheck
@close="showEditWinSvcCheck = false"
:editCheckPK="editCheckPK"
<q-dialog v-model="showWinSvcCheck">
<WinSvcCheck
@close="showWinSvcCheck = false"
:agentpk="selectedAgentPk"
:mode="mode"
:checkpk="checkpk"
/>
</q-dialog>
<!-- refactor below -->
<!-- script check -->
<q-dialog v-model="showAddScriptCheck">
<AddScriptCheck @close="showAddScriptCheck = false" :agentpk="selectedAgentPk" />
@@ -253,9 +251,8 @@ import DiskSpaceCheck from "@/components/modals/checks/DiskSpaceCheck";
import MemCheck from "@/components/modals/checks/MemCheck";
import CpuLoadCheck from "@/components/modals/checks/CpuLoadCheck";
import PingCheck from "@/components/modals/checks/PingCheck";
import WinSvcCheck from "@/components/modals/checks/WinSvcCheck";
// refactor below
import AddWinSvcCheck from "@/components/modals/checks/AddWinSvcCheck";
import EditWinSvcCheck from "@/components/modals/checks/EditWinSvcCheck";
import AddScriptCheck from "@/components/modals/checks/AddScriptCheck";
import EditScriptCheck from "@/components/modals/checks/EditScriptCheck";
import ScriptOutput from "@/components/modals/checks/ScriptOutput";
@@ -270,8 +267,7 @@ export default {
MemCheck,
CpuLoadCheck,
PingCheck,
AddWinSvcCheck,
EditWinSvcCheck,
WinSvcCheck,
AddScriptCheck,
EditScriptCheck,
ScriptOutput,
@@ -288,9 +284,8 @@ export default {
showMemCheck: false,
showCpuLoadCheck: false,
showPingCheck: false,
showWinSvcCheck: false,
// refactor below
showAddWinSvcCheck: false,
showEditWinSvcCheck: false,
showAddScriptCheck: false,
showEditScriptCheck: false,
showScriptOutput: false,
@@ -349,6 +344,9 @@ export default {
case "ping":
this.showPingCheck = true;
break;
case "winsvc":
this.showWinSvcCheck = true;
break;
}
},
checkAlertAction(pk, category, alert_type, alert_action) {

View File

@@ -1,182 +0,0 @@
<template>
<q-card style="min-width: 400px">
<q-card-section class="row items-center">
<div class="text-h6">Add Windows Service Check</div>
<q-space />
<q-btn icon="close" flat round dense v-close-popup />
</q-card-section>
<q-form @submit.prevent="addCheck">
<q-card-section>
<q-radio
v-if="policypk"
v-model="serviceType"
val="svcdefault"
label="Choose from defaults"
@input="manualServiceName = null; manualSvcDisplayName = null; displayName = null"
/>
<q-radio
v-if="policypk"
v-model="serviceType"
val="svcmanual"
label="Enter manually"
@input="manualServiceName = null; manualSvcDisplayName = null; displayName = null"
/>
<q-select
v-if="policypk && serviceType === 'svcdefault'"
dense
outlined
v-model="displayName"
:options="svcDisplayNames"
label="Service"
@input="getRawName"
/>
<q-input
v-if="policypk && serviceType === 'svcmanual'"
outlined
dense
v-model="manualServiceName"
label="Service Name"
/>
<q-input
v-if="policypk && serviceType === 'svcmanual'"
outlined
dense
v-model="manualSvcDisplayName"
label="Display Name"
/>
<q-select
v-if="agentpk"
:rules="[val => !!val || '*Required']"
dense
outlined
v-model="displayName"
:options="svcDisplayNames"
label="Service"
@input="getRawName"
/>
</q-card-section>
<q-card-section>
<q-checkbox
v-model="passIfStartPending"
label="PASS if service is in 'Start Pending' mode"
/>
<q-checkbox v-model="restartIfStopped" label="RESTART service if it's stopped" />
</q-card-section>
<q-card-section>
<q-select
outlined
dense
v-model="failure"
:options="failures"
label="Number of consecutive failures before alert"
/>
</q-card-section>
<q-card-actions align="right">
<q-btn label="Add" color="primary" type="submit" />
<q-btn label="Cancel" v-close-popup />
</q-card-actions>
</q-form>
</q-card>
</template>
<script>
import axios from "axios";
import { mapState } from "vuex";
import mixins from "@/mixins/mixins";
export default {
name: "AddWinSvcCheck",
props: ["agentpk", "policypk"],
mixins: [mixins],
data() {
return {
serviceType: "svcdefault",
manualServiceName: "",
manualSvcDisplayName: "",
servicesData: [],
displayName: "",
rawName: [],
passIfStartPending: false,
restartIfStopped: false,
failure: 2,
failures: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
};
},
computed: {
svcDisplayNames() {
return this.servicesData.map(k => k.display_name).sort();
}
},
methods: {
getServices() {
if (this.policypk) {
axios.get("/services/getdefaultservices/").then(r => {
this.servicesData = Object.freeze(r.data);
});
} else {
axios.get(`/services/${this.agentpk}/services/`).then(r => {
this.servicesData = Object.freeze([r.data][0].services);
});
}
},
getRawName() {
let svc = this.servicesData.find(k => k.display_name === this.displayName);
this.rawName = [svc].map(j => j.name);
},
addCheck() {
const pk = this.policypk ? { policy: this.policypk } : { pk: this.agentpk };
let rawname, displayname;
if (this.policypk) {
// policy
if (this.serviceType === "svcdefault") {
rawname = { rawname: this.rawName[0] };
displayname = { displayname: this.displayName };
if (this.rawName.length === 0) {
this.notifyError("Please select a service");
return;
}
} else if (this.serviceType === "svcmanual") {
rawname = { rawname: this.manualServiceName };
displayname = { displayname: this.manualSvcDisplayName };
if (!this.manualServiceName || !this.manualSvcDisplayName) {
this.notifyError("All fields required");
return;
}
}
} else {
// agent
rawname = { rawname: this.rawName[0] };
displayname = { displayname: this.displayName };
}
const data = {
...pk,
check_type: "winsvc",
...rawname,
...displayname,
passifstartpending: this.passIfStartPending,
restartifstopped: this.restartIfStopped,
failures: this.failure
};
axios
.post("/checks/addstandardcheck/", data)
.then(r => {
this.$emit("close");
if (this.policypk) {
this.$store.dispatch("automation/loadPolicyChecks", this.policypk);
} else {
this.$store.dispatch("loadChecks", this.agentpk);
}
this.notifySuccess(`${data.displayname} service check added!`);
})
.catch(e => this.notifyError(e.response.data.error));
}
},
mounted() {
this.getServices();
}
};
</script>

View File

@@ -1,103 +0,0 @@
<template>
<q-card style="min-width: 400px">
<q-card-section class="row items-center">
<div class="text-h6">Edit Windows Service Check</div>
<q-space />
<q-btn icon="close" flat round dense v-close-popup />
</q-card-section>
<q-form @submit.prevent="editCheck">
<q-card-section>
<q-select
disable
dense
outlined
v-model="displayName"
:options="svcDisplayNames"
label="Service"
/>
</q-card-section>
<q-card-section>
<q-checkbox
v-model="passIfStartPending"
label="PASS if service is in 'Start Pending' mode"
/>
<q-checkbox v-model="restartIfStopped" label="RESTART service if it's stopped" />
</q-card-section>
<q-card-section>
<q-select
outlined
dense
v-model="failure"
:options="failures"
label="Number of consecutive failures before alert"
/>
</q-card-section>
<q-card-actions align="right">
<q-btn label="Edit" color="primary" type="submit" />
<q-btn label="Cancel" v-close-popup />
</q-card-actions>
</q-form>
</q-card>
</template>
<script>
import axios from "axios";
import { mapState } from "vuex";
import mixins from "@/mixins/mixins";
export default {
name: "EditWinSvcCheck",
props: ["agentpk", "editCheckPK", "policypk"],
mixins: [mixins],
data() {
return {
displayName: "",
svcDisplayNames: [],
passIfStartPending: null,
restartIfStopped: null,
failure: null,
failures: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
};
},
methods: {
async getService() {
try {
let r = await axios.get(`/checks/getstandardcheck/winsvc/${this.editCheckPK}/`);
this.svcDisplayNames = [r.data.svc_display_name];
this.displayName = r.data.svc_display_name;
this.passIfStartPending = r.data.pass_if_start_pending;
this.restartIfStopped = r.data.restart_if_stopped;
this.failure = r.data.failures;
} catch (e) {
console.log(`ERROR!: ${e}`);
}
},
editCheck() {
const data = {
pk: this.editCheckPK,
check_type: "winsvc",
failures: this.failure,
passifstartpending: this.passIfStartPending,
restartifstopped: this.restartIfStopped
};
axios
.patch("/checks/editstandardcheck/", data)
.then(r => {
this.$emit("close");
if (this.policypk) {
this.$store.dispatch("automation/loadPolicyChecks", this.policypk);
} else {
this.$store.dispatch("loadChecks", this.agentpk);
}
this.notifySuccess("Windows service check was edited!");
})
.catch(e => this.notifyError(e.response.data.error));
}
},
mounted() {
this.getService();
}
};
</script>

View File

@@ -0,0 +1,215 @@
<template>
<q-card style="min-width: 40vw">
<q-card-section class="row items-center">
<div v-if="mode === 'add'" class="text-h6">Add Service Check</div>
<div v-else-if="mode === 'edit'" class="text-h6">Edit Service Check</div>
<q-space />
<q-btn icon="close" flat round dense v-close-popup />
</q-card-section>
<q-form @submit.prevent="mode === 'add' ? addCheck() : editCheck()">
<q-card-section>
<!-- policy check, either choose from a list of default services or enter manually -->
<q-radio
v-if="policypk && this.mode !== 'edit'"
v-model="winsvccheck.svc_policy_mode"
val="default"
label="Choose from defaults"
@input="clearServiceOptions"
/>
<q-radio
v-if="policypk && this.mode !== 'edit'"
v-model="winsvccheck.svc_policy_mode"
val="manual"
label="Enter manually"
@input="clearServiceOptions"
/>
<q-select
v-if="policypk && winsvccheck.svc_policy_mode === 'default' && this.mode !== 'edit'"
:rules="[val => !!val || '*Required']"
dense
outlined
v-model="winsvccheck.svc_name"
:options="serviceOptions"
label="Service"
map-options
emit-value
@input="getDisplayName"
/>
<!-- disable selection if editing -->
<q-select
v-if="policypk && winsvccheck.svc_policy_mode === 'default' && this.mode === 'edit'"
disable
dense
outlined
v-model="winsvccheck.svc_name"
:options="serviceOptions"
label="Service"
map-options
emit-value
/>
<q-input
v-if="policypk && winsvccheck.svc_policy_mode === 'manual'"
:rules="[val => !!val || '*Required']"
outlined
dense
v-model="winsvccheck.svc_name"
label="Service Name"
/>
<q-input
v-if="policypk && winsvccheck.svc_policy_mode === 'manual'"
:rules="[val => !!val || '*Required']"
outlined
dense
v-model="winsvccheck.svc_display_name"
label="Display Name"
/>
<!-- agent check -->
<!-- disable selection if editing -->
<q-select
v-if="agentpk"
:rules="[val => !!val || '*Required']"
dense
outlined
v-model="winsvccheck.svc_name"
:options="serviceOptions"
label="Service"
map-options
emit-value
@input="getDisplayName"
:disable="this.mode === 'edit'"
/>
</q-card-section>
<q-card-section>
<q-checkbox
v-model="winsvccheck.pass_if_start_pending"
label="PASS if service is in 'Start Pending' mode"
/>
<br />
<q-checkbox
v-model="winsvccheck.restart_if_stopped"
label="Restart service if it's stopped"
/>
</q-card-section>
<q-card-section>
<q-select
outlined
dense
v-model="winsvccheck.fails_b4_alert"
:options="failOptions"
label="Number of consecutive failures before alert"
/>
</q-card-section>
<q-card-actions align="right">
<q-btn v-if="mode === 'add'" label="Add" color="primary" type="submit" />
<q-btn v-else-if="mode === 'edit'" label="Edit" color="primary" type="submit" />
<q-btn label="Cancel" v-close-popup />
</q-card-actions>
</q-form>
</q-card>
</template>
<script>
import axios from "axios";
import mixins from "@/mixins/mixins";
import { mapGetters } from "vuex";
export default {
name: "WinSvcCheck",
props: {
agentpk: Number,
policypk: Number,
mode: String,
checkpk: Number
},
mixins: [mixins],
data() {
return {
winsvccheck: {
check_type: "winsvc",
svc_name: null,
svc_display_name: null,
svc_policy_mode: null,
pass_if_start_pending: false,
restart_if_stopped: false,
fails_b4_alert: 1
},
svcData: [],
failOptions: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
};
},
computed: {
...mapGetters(["agentServices"]),
serviceOptions() {
const ret = [];
this.svcData.forEach(i => {
ret.push({ label: i.display_name, value: i.name });
});
// sort alphabetically by display name
return ret.sort((a, b) => (a.label > b.label ? 1 : -1));
}
},
methods: {
clearServiceOptions() {
this.winsvccheck.svc_name = null;
this.winsvccheck.svc_display_name = null;
},
setServices() {
if (this.policypk) {
axios.get("/services/defaultservices/").then(r => {
this.svcData = Object.freeze(r.data);
this.winsvccheck.svc_policy_mode = "default";
});
} else {
this.svcData = Object.freeze(this.agentServices);
}
},
getDisplayName() {
this.winsvccheck.svc_display_name = this.serviceOptions.find(i => i.value === this.winsvccheck.svc_name).label;
},
getCheck() {
axios.get(`/checks/${this.checkpk}/check/`).then(r => (this.winsvccheck = r.data));
},
addCheck() {
const pk = this.policypk ? { policy: this.policypk } : { pk: this.agentpk };
const data = {
...pk,
check: this.winsvccheck
};
axios
.post("/checks/checks/", data)
.then(r => {
this.$emit("close");
this.reloadChecks();
this.notifySuccess(r.data);
})
.catch(e => this.notifyError(e.response.data.non_field_errors));
},
editCheck() {
axios
.patch(`/checks/${this.checkpk}/check/`, this.winsvccheck)
.then(r => {
this.$emit("close");
this.reloadChecks();
this.notifySuccess(r.data);
})
.catch(e => this.notifyError(e.response.data.non_field_errors));
},
reloadChecks() {
if (this.policypk) {
this.$store.dispatch("automation/loadPolicyChecks", this.policypk);
} else {
this.$store.dispatch("loadChecks", this.agentpk);
}
}
},
created() {
this.setServices();
},
mounted() {
if (this.mode === "edit") {
this.getCheck();
}
}
};
</script>

View File

@@ -42,6 +42,9 @@ export const store = new Vuex.Store({
agentDisks(state) {
return state.agentSummary.disks;
},
agentServices(state) {
return state.agentSummary.services;
},
checks(state) {
return state.agentChecks;
},