amnuts / opcache-gui

A clean, effective and responsive interface for Zend OPcache
1.23k stars 198 forks source link

OpCache data in Home Assistant #109

Closed cvc90 closed 3 months ago

cvc90 commented 4 months ago

Hi @amnuts,

I want to import OpCache data into Home Assistant via multiscrape, is there any method to view the data in .json format?

amnuts commented 4 months ago

What a really interesting idea, @cvc90!

It doesn't have a built in method, as such... The main method that spits out the details is getData. Where that's use, within handle() and the main template itself when outputting the main React component, it just wraps the output of getData() in PHP's own json_encode().

The handle() method it will output json format under a couple conditions; if it's a json request and the allow_realtime option is set. So you could try to take advantage of that, but it might just be easier to add your own function to wrap around getData()?

Here's where it's called currently in the code:

which end up combined as:

cvc90 commented 3 months ago

Hi @amnuts,

After investigating your answer for several days and analyzing how you were importing this ‘.json’ data, I have found a possible solution to import this data to ‘Home Assistant’ or any program that is compatible with ‘.json’ from opcache-gui without having to modify anything.

Here is a summary:

To be able to visualize the data in ‘.json’ format it would be as simple as making a request with the following ‘header’

headers: Accept: ‘application/json, text/plain, /

Here is an example of making a request using curl to return the ‘.json’ file

curl ‘https://url/opcache-gui.php’ -H ‘accept: application/json, text/plain, /

If what we want is to import this data to ‘Home Assistant’ there are several methods for importing ‘.json’ data but I think the most efficient to not saturate requests and from the same request to collect the data would be using the ‘HA MultiScrape custom component’.

Here is an example of how it would be to collect data from some of the variables

A sample of data from ‘Home Assistant’, from one of my servers to which I have already implemented this integration.

image

What do you think about this integration? There are other methods but I think it is the easiest / cleanest to implement.

What do you think?

amnuts commented 3 months ago

I only have speaker-based assistants, no screens, so this is not something I've ever loomed into (though as a coincidence, I did book the HA details a couple months back to look into to controlling some lighting via an arduino... not that I have, yet!), so I can't really speak as to whether it's the best way forwards or not.

However, with a quick scan on the forums, it does look like a bunch of people on there are getting json data in a similar way with multiscrape. So if it works for you, and from the screenshot I'm going to say, yes, it does 😄, then I think your solution is great!

but I think that if it works for you then you're on to a winner! :-)

cvc90 commented 3 months ago

Even if you only have "speaker-based assistants", by adding each tribute as a sensor in "HA", you can use it from the "speaker-based assistants" to tell you the status of each sensor.

You can even create your own automations to send you messages through the "HA" application, messaging systems like Telegram or even voice messages for your "speaker-based assistants" such as for status checks or even if it detects that some value does not match what is established.

I have to say that this integration can be very useful...

After several days and including them in several "Home Assistants", they work perfectly without any problem.