handle ping + update script

*handle ping on  Instance
*check global config too for is_used
*update bunkernet and redis actions.py
*handle more keys for status
*core plugin menu doesn't open a new tab anymore
This commit is contained in:
Jordan Blasenhauer 2024-02-05 13:05:55 +01:00
parent 427b7efc91
commit a97f90e713
9 changed files with 152 additions and 216 deletions

View file

@ -1,8 +1,6 @@
def bunkernet():
return {
"message": "ok",
"data": {
"info": "test",
"status": "active",
},
}
def bunkernet(**kwargs):
try:
ping_data = kwargs["app"].config["INSTANCES"].get_ping("bunkernet")
return {"ping_status": ping_data["status"]}
except:
return {"ping_status": "error"}

View file

@ -164,7 +164,7 @@
type: "text",
},
// value : active / inactive / unknown
status: {
ping_status: {
el: document.querySelector("[data-status-svg]"),
value: "unknown",
type: "status",
@ -173,36 +173,6 @@
});
</script>
<!-- end test -->
<script async>
function ping() {
let data = new FormData();
data.set("csrf_token", "{{ csrf_token() }}");
let xhr = new XMLHttpRequest();
xhr.open("POST", "{{ url_for('plugins') }}/bunkernet", true);
xhr.send(data);
xhr.onload = function () {
document.getElementById("no-test").classList.add("hidden");
if (xhr.status == 200) {
document.getElementById("response-failed").classList.remove("hidden");
document.getElementById("response-none").classList.add("hidden");
document.getElementById("response-success").classList.add("hidden");
} else if (xhr.status == 403) {
document
.getElementById("response-success")
.classList.remove("hidden");
document.getElementById("response-none").classList.add("hidden");
document.getElementById("response-failed").classList.add("hidden");
} else {
document.getElementById("response-none").classList.remove("hidden");
document.getElementById("response-success").classList.add("hidden");
document.getElementById("response-failed").classList.add("hidden");
}
};
}
</script>
{% else %}
<div
class="h-fit transition hover:scale-102 col-span-12 md:col-span-6 2xl:col-span-4 3xl:col-span-3 p-4 relative min-w-0 break-words bg-white shadow-xl dark:bg-slate-850 dark:shadow-dark-xl rounded-2xl bg-clip-border"

View file

@ -1,8 +1,20 @@
def redis():
return {
"message": "ok",
"data": {
"info": "test",
"status": "active",
},
}
def redis(**kwargs):
ping = {}
data = {}
try:
ping_data = kwargs["app"].config["INSTANCES"].get_ping("redis")
ping = {"ping_status": ping_data["status"]}
except:
ping = {"ping_status": "error"}
try:
metrics = kwargs["app"].config["INSTANCES"].get_metrics("antibot")
if metrics.get("redis_nb_keys") is None:
metrics["redis_nb_keys"] = 0
data = metrics
except:
data = {"redis_nb_keys": 0}
return {**ping, **data}

View file

@ -12,87 +12,43 @@
<!-- status -->
<div class="col-span-12 grid grid-cols-12 gap-4">
<!-- info-->
<div
class="col-span-12 md:col-span-6 2xl:col-span-3 3xl:col-span-2 w-fit h-fit transition hover:scale-102 p-4 relative min-w-0 break-words bg-white shadow-xl dark:bg-slate-850 dark:shadow-dark-xl rounded-2xl bg-clip-border"
class="h-fit transition hover:scale-102 col-span-12 md:col-span-6 2xl:col-span-4 3xl:col-span-3 p-4 relative min-w-0 break-words bg-white shadow-xl dark:bg-slate-850 dark:shadow-dark-xl rounded-2xl bg-clip-border"
>
<div class="mx-1 flex justify-start items-center">
<h5 class="mb-0 font-bold dark:text-white/90 mr-4">STATUS</h5>
<svg
data-status-svg
class="w-6 h-6"
viewBox="0 0 100 100"
xmlns="http://www.w3.org/2000/svg"
>
<circle cx="50" cy="50" r="50" />
</svg>
<h5 class="mb-2 font-bold dark:text-white/90">INFO</h5>
<div class="mx-1 flex justify-start items-center my-4">
<p
data-info
class="transition duration-300 ease-in-out mb-0 font-sans text-sm leading-normal dark:text-gray-500 dark:opacity-80"
></p>
</div>
<p
data-status-text
class="mx-1 transition duration-300 ease-in-out mb-0 font-sans text-sm leading-normal dark:text-gray-500 dark:opacity-80"
></p>
</div>
<!-- end status -->
</div>
<!-- info-->
<div
class="h-fit transition hover:scale-102 col-span-12 md:col-span-6 2xl:col-span-4 3xl:col-span-3 p-4 relative min-w-0 break-words bg-white shadow-xl dark:bg-slate-850 dark:shadow-dark-xl rounded-2xl bg-clip-border"
>
<h5 class="mb-2 font-bold dark:text-white/90">INFO</h5>
<div class="mx-1 flex justify-start items-center my-4">
<p
data-info
class="transition duration-300 ease-in-out mb-0 font-sans text-sm leading-normal dark:text-gray-500 dark:opacity-80"
></p>
</div>
</div>
<!-- end info -->
<!-- test-->
<div
class="transition hover:scale-102 col-span-12 md:col-span-6 2xl:col-span-4 3xl:col-span-3 p-4 relative min-w-0 break-words bg-white shadow-xl dark:bg-slate-850 dark:shadow-dark-xl rounded-2xl bg-clip-border"
>
<h5 class="mb-2 font-bold dark:text-white/90">TEST</h5>
<p
class="my-2 transition duration-300 ease-in-out mb-0 font-sans text-sm leading-normal dark:text-gray-500 dark:opacity-80 text-center"
<!-- end info -->
<div
class="h-fit dark:brightness-110 max-h-none sm:max-h-28 hover:scale-102 transition col-span-12 md:col-span-6 2xl:col-span-4 flex p-4 justify-between w-full shadow-md break-words bg-white dark:bg-slate-850 dark:shadow-dark-xl rounded-2xl bg-clip-border"
>
Use the next button to ping Redis.
</p>
<div class="flex justify-center mt-4">
<button type="button" class="edit-btn text-sm" onclick="ping()">
Ping Redis
</button>
</div>
<hr
class="h-px mx-0 mt-3 mb-2 bg-transparent bg-gradient-to-r from-transparent via-black/40 to-transparent dark:bg-gradient-to-r dark:from-transparent dark:via-white dark:to-transparent"
/>
<div id="response-div" class="flex justify-center items-center">
<div id="no-test" class="flex justify-center items-center">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6 stroke-gray-600"
<!-- text -->
<div>
<p
class="mb-2 font-sans text-sm font-semibold leading-normal uppercase dark:text-white dark:opacity-60"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="m21 21-5.197-5.197m0 0A7.5 7.5 0 1 0 5.196 5.196a7.5 7.5 0 0 0 10.607 10.607Z"
/>
</svg>
<p class="mb-0 ml-1 font-semibold text-gray-600 text-base uppercase">
UNKNOWN
Keys
</p>
<h5 data-count class="mb-1 font-bold dark:text-white/90">"unknown"</h5>
<p class="mb-0 dark:text-white dark:opacity-60">
<span class="font-bold leading-normal text-sm textsky-500 mx-0.5"
>total number
</span>
</p>
</div>
<!-- end text -->
<!-- icon -->
<div
class="hidden flex justify-center items-center"
id="response-success"
role="img"
class="dark:brightness-90 inline-block w-12 h-12 text-center rounded-circle bg-sky-700"
>
<svg
xmlns="http://www.w3.org/2000/svg"
@ -100,60 +56,40 @@
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6 stroke-green-500"
class="scale-[0.6] leading-none text-lg relative fill-sky-700 stroke-white"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M9 12.75 11.25 15 15 9.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"
d="M3.75 12h16.5m-16.5 3.75h16.5M3.75 19.5h16.5M5.625 4.5h12.75a1.875 1.875 0 0 1 0 3.75H5.625a1.875 1.875 0 0 1 0-3.75Z"
/>
</svg>
<p class="mb-0 ml-1 font-semibold text-green-500 text-base uppercase">
SUCCESS
</p>
</div>
<div class="hidden flex justify-center items-center" id="response-failed">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6 stroke-red-500"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M12 9v3.75m9-.75a9 9 0 1 1-18 0 9 9 0 0 1 18 0Zm-9 3.75h.008v.008H12v-.008Z"
/>
</svg>
<p class="mb-0 ml-1 font-semibold text-red-500 text-base uppercase">
FAILED
</p>
</div>
<div class="hidden flex justify-center items-center" id="response-none">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6 stroke-red-500"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M18.364 18.364A9 9 0 0 0 5.636 5.636m12.728 12.728A9 9 0 0 1 5.636 5.636m12.728 12.728L5.636 5.636"
/>
</svg>
<p class="mb-0 ml-1 font-semibold text-red-500 text-base uppercase">
FAILED
</p>
</div>
<!-- end icon -->
</div>
</div>
<div
class="col-span-12 md:col-span-6 2xl:col-span-3 3xl:col-span-2 w-fit h-fit transition hover:scale-102 p-4 relative min-w-0 break-words bg-white shadow-xl dark:bg-slate-850 dark:shadow-dark-xl rounded-2xl bg-clip-border"
>
<div class="mx-1 flex justify-start items-center">
<h5 class="mb-0 font-bold dark:text-white/90 mr-4">STATUS</h5>
<svg
data-status-svg
class="w-6 h-6"
viewBox="0 0 100 100"
xmlns="http://www.w3.org/2000/svg"
>
<circle cx="50" cy="50" r="50" />
</svg>
</div>
<p
data-status-text
class="mx-1 transition duration-300 ease-in-out mb-0 font-sans text-sm leading-normal dark:text-gray-500 dark:opacity-80"
></p>
</div>
<!-- end status -->
<script>
// Use SetupPlugin class that is on static/js/plugins/setup.js
const setPlugin = new SetupPlugin({
@ -162,8 +98,13 @@
value: "{{ plugin['description'] or ''}}",
type: "text",
},
redis_nb_keys: {
el: document.querySelector("[data-count]"),
value: "unknown",
type: "text",
},
// value : active / inactive / unknown
status: {
ping_status: {
el: document.querySelector("[data-status-svg]"),
value: "unknown",
type: "status",
@ -172,35 +113,6 @@
});
</script>
<script async>
function ping() {
let data = new FormData();
data.set("csrf_token", "{{ csrf_token() }}");
let xhr = new XMLHttpRequest();
xhr.open("POST", "{{ url_for('plugins') }}/bunkernet", true);
xhr.send(data);
xhr.onload = function () {
document.getElementById("no-test").classList.add("hidden");
if (xhr.status == 200) {
document.getElementById("response-failed").classList.remove("hidden");
document.getElementById("response-none").classList.add("hidden");
document.getElementById("response-success").classList.add("hidden");
} else if (xhr.status == 403) {
document
.getElementById("response-success")
.classList.remove("hidden");
document.getElementById("response-none").classList.add("hidden");
document.getElementById("response-failed").classList.add("hidden");
} else {
document.getElementById("response-none").classList.remove("hidden");
document.getElementById("response-success").classList.add("hidden");
document.getElementById("response-failed").classList.add("hidden");
}
};
}
</script>
{% else %}
<div
class="h-fit transition hover:scale-102 col-span-12 md:col-span-6 2xl:col-span-4 3xl:col-span-3 p-4 relative min-w-0 break-words bg-white shadow-xl dark:bg-slate-850 dark:shadow-dark-xl rounded-2xl bg-clip-border"

View file

@ -49,21 +49,6 @@
role="img"
class="dark:brightness-90 inline-block w-12 h-12 text-center rounded-circle bg-green-700"
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="m4.5 12.75 6 6 9-13.5"
/>
</svg>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"

View file

@ -1269,6 +1269,7 @@ def custom_plugin(plugin: str):
break
# Get USE_<NAME> and check if the plugin is used by one service
config = app.config["CONFIG"].get_config(methods=False)
services = app.config["CONFIG"].get_services(with_drafts=True)
use_key = False
is_used = False
@ -1276,7 +1277,9 @@ def custom_plugin(plugin: str):
if key.upper().startswith("USE_"):
use_key = key
if use_key:
is_used = True if config[use_key] == "yes" else False
if use_key and not is_used:
for service in services:
if service[use_key] == "yes":
is_used = True
@ -1477,7 +1480,9 @@ def logs_linux():
log_lower = log.lower()
error_type = (
"error" if "[error]" in log_lower or "[crit]" in log_lower or "[alert]" in log_lower or "" in log_lower else ("warn" if "[warn]" in log_lower or "⚠️" in log_lower else ("info" if "[info]" in log_lower or "" in log_lower else "message"))
"error"
if "[error]" in log_lower or "[crit]" in log_lower or "[alert]" in log_lower or "" in log_lower
else ("warn" if "[warn]" in log_lower or "⚠️" in log_lower else ("info" if "[info]" in log_lower or "" in log_lower else "message"))
)
logs.append(

View file

@ -120,6 +120,12 @@ class Instance:
def metrics(self, plugin_id) -> Tuple[bool, dict[str, Any]]:
return self.apiCaller.send_to_apis("GET", f"/metrics/{plugin_id}", response=True)
def metrics_redis(self) -> Tuple[bool, dict[str, Any]]:
return self.apiCaller.send_to_apis("GET", "/redis/stats", response=True)
def ping(self, plugin_id, hostname: Optional[str] = "") -> Tuple[bool, dict[str, Any]]:
return self.apiCaller.send_to_apis("POST", f"/{plugin_id}/ping{'?host={hostname}' if hostname else ''}", response=True)
class Instances:
def __init__(self, docker_client, kubernetes_client, integration: str):
@ -376,7 +382,10 @@ class Instances:
metrics = {}
for instance in self.get_instances():
try:
resp, instance_metrics = instance.metrics(plugin_id)
if plugin_id == "redis":
resp, instance_metrics = instance.metrics_redis()
else:
resp, instance_metrics = instance.metrics(plugin_id)
except:
continue
@ -423,3 +432,30 @@ class Instances:
metrics[key][k] = v
continue
return metrics
def get_ping(self, plugin_id: str):
# Need at least one instance to get a success ping to return success
ping = {"status": "error"}
for instance in self.get_instances():
try:
if plugin_id == "redis":
resp, ping_data = instance.ping(plugin_id, instance["name"])
else:
resp, ping_data = instance.ping(plugin_id)
print("res")
print(resp)
print(ping_data)
except:
continue
if not resp:
continue
if instance.name not in ping_data or ping_data[instance.name]["msg"] is None or ping_data[instance.name]["msg"] is not dict:
continue
if ping_data[instance.name]["status"] == "success":
ping["status"] = "success"
break
return ping

View file

@ -180,16 +180,35 @@ class SetupPlugin {
el.textContent = value || "";
continue;
}
// Case status
if (type === "status") {
const textEl = val["textEl"] || null;
if (value === "active")
if (
value === "active" ||
value === "up" ||
value === "yes" ||
value === "success" ||
value === "true"
) {
this.setStatus(el, textEl, "fill-green-500", "Active");
if (value === "inactive")
continue;
}
if (
value === "inactive" ||
value === "down" ||
value === "no" ||
value === "error" ||
value === "false"
) {
this.setStatus(el, textEl, "fill-red-500", "Inactive");
if (value === "unknown")
this.setStatus(el, textEl, "fill-sky-500", "Unknown");
continue;
}
//default
this.setStatus(el, textEl, "fill-sky-500", "Unknown");
continue;
}

View file

@ -447,7 +447,6 @@
{% endif %} {% for plugin in plugins %} {% if plugin['page'] %}
<li class="mt-0.5 w-full">
<a
target="_blank"
class="{% if current_endpoint == 'logs' %}font-semibold text-slate-700 dark:bg-primary/50 rounded-lg dark:hover:bg-primary/60 bg-primary/20 hover:bg-primary/30{% else %}dark:hover:bg-primary/20 hover:bg-primary/5 {% endif %} hover:rounded-lg dark:text-white dark:opacity-80 py-1 text-sm ease-nav-brand my-0 mx-2 flex items-center whitespace-nowrap px-4 transition"
href="{{request.url_root}}plugins/{{plugin['id']}}"
>