Jump to content

Plugin Endpoints (CPU - Server Uptime)


chef

Recommended Posts

I wrote this plugin that will add endpoints to the Emby API that will return information on Linux, Android and Windows (not sure about Mac) that extends SystemInfo.

 

It creates CpuLoadService

 

"/emby/GetSystemUptimeData" 

{
  "UpTimeDays": "36",
  "UpTimeHours": "15"
}

"/emby/GetCpuUsageData"

{
  "CpuUsage": "0"
}

Might be useful.

 

DOWNLOAD: CPULoad.zip

Here is the github repo in case some wants to added some extra info to the endpoints

 

https://github.com/chefbennyj1/Emby.CPULoad

Edited by chef
  • Like 1
Link to comment
Share on other sites

chacawaca
Dont work on ubuntu.

Browser console event :

 

VM129:1 Fetch API cannot load wss://xxxxxxxx/emby/OpenProcessorEvent. URL scheme must be "http" or "https" for CORS request.

(anonymous) @ VM129:1

VM129:1 Uncaught (in promise) TypeError: Failed to fetch

    at <anonymous>:1:876

    at getFetchPromise (apiclient.js?v=4.3.1.0:1)

    at ApiClient.fetch (apiclient.js?v=4.3.1.0:1)

    at ApiClient.getJSON (apiclient.js?v=4.3.1.0:1)

    at HTMLDivElement.<anonymous> (configurationpage?name=CpuLoadPluginConfigurationPageJS&v=4.3.1.0:9)

    at onViewChange (viewmanager.js?v=4.3.1.0:1)

    at viewmanager.js?v=4.3.1.0:1

Link to comment
Share on other sites

What about in the swagger?

 

I'm thinking maybe did you forget to use an API key when accessing that URL endpoint?

Link to comment
Share on other sites

Actually, you're right, I accidentally left in the configuration page. This currently shouldn't have any pages. It's mostly just adding the endpoints to the API :)

Link to comment
Share on other sites

chacawaca

hmm ok i understand why swagger ui dont work, it try to use local port instead of public port

  • Like 1
Link to comment
Share on other sites

CBers

@@Luke is there a way to add uptime to home page ?

 

Not at at this time, but it's possible for the future. Thanks.

 

I think something similar was suggested at the very beginning (5+ years ago) of MB3/EMBY, but has never seen the light of day.

Link to comment
Share on other sites

@@chacawaca

 

 

Here is how you do it

 

1. Make sure the plugin is installed in your plugins folder from the OP

 

2. Open "\Emby-Server\system\dashboard-ui\dashboard.html"

 

3. Find the html lines:

                        <p id="ports"></p>
                        
                        <p class="localUrl"></p>

add this upTime paragraph element so it looks like this:

                        <p id="ports"></p>
                        <p id="upTime"></p>
                        <p class="localUrl"></p>

Save it!

 

4. Open index.html

 

After the "apploader.js" script tags add a new script tags

 

It will look like this:

<script defer src="apploader.js"></script>
<script>

</script>

put this code into the script tags:

(function(win) {
    'use strict';

    var listeners = [],
        doc = win.document,
        MutationObserver = win.MutationObserver || win.WebKitMutationObserver,
        observer;



    function ready(selector, fn) {
        // Store the selector and callback to be monitored
        listeners.push({
            selector: selector,
            fn: fn
        });
        if (!observer) {
            // Watch for changes in the document
            observer = new MutationObserver(function(mutations) {

                check()
            });
            observer.observe(doc.documentElement, {
                childList: true,
                subtree: true,
                attributes: true,
                attributeOldValue: true,
            });
        }
        // Check if the element is currently in the DOM
        check();
    }



    function check() {
        // Check the DOM for elements matching a stored selector
        for (var i = 0, len = listeners.length, listener, elements; i < len; i++) {
            listener = listeners[i];
            // Query for elements matching the specified selector
            elements = doc.querySelectorAll(listener.selector);
            for (var j = 0, jLen = elements.length, element; j < jLen; j++) {
                element = elements[j];

                // Make sure the callback isn't invoked with the 
                // same element more than once
                if (!element.ready) {
                    element.ready = true;
                    // Invoke the callback with the element
                    listener.fn.call(element, element);
                }
            }
        }
    }

    // Expose `ready`
    win.ready = ready;

})(this);

var your_emby_server_Ip = ""; //Put your internal IP here

ready("#upTime", (element) => {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'http://' + your_emby_server_Ip + '/emby/GetSystemUptimeData');

        // Track the state changes of the request.
        xhr.onreadystatechange = function() {

            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    var json = JSON.parse(xhr.responseText);
                    element.innerHTML = 'Up Time: ' + json['UpTimeDays'] + ' Days ' + json['UpTimeHours'] + ' Hours'
                } else {
                    console.log('Error: ' + xhr.status); // An error occurred during the request.
                }
            }
        };
      
        xhr.send();
    })

Note: a couple things to notice here 

 

1. Fill in your Server internal IP

2. It will only work internally. (I will create something that works externally as well)

3. Notice there is no API Key involved that means its open to the wild.  I'll create a fix in the plugin that needs admin keys to get the data, but for now that's how you do it.

Edited by chef
  • Like 1
Link to comment
Share on other sites

This bit of code will work best (make sure the plugin is installed at mentioned at the top)

(function(win) {
    'use strict';

    var listeners = [],
        doc = win.document,
        MutationObserver = win.MutationObserver || win.WebKitMutationObserver,
        observer;



    function ready(selector, fn) {
        // Store the selector and callback to be monitored
        listeners.push({
            selector: selector,
            fn: fn
        });
        if (!observer) {
            // Watch for changes in the document
            observer = new MutationObserver(function(mutations) {

                check()
            });
            observer.observe(doc.documentElement, {
                childList: true,
                subtree: true,
                attributes: true,
                attributeOldValue: true,
            });
        }
        // Check if the element is currently in the DOM
        check();
    }



    function check() {
        // Check the DOM for elements matching a stored selector
        for (var i = 0, len = listeners.length, listener, elements; i < len; i++) {
            listener = listeners[i];
            // Query for elements matching the specified selector
            elements = doc.querySelectorAll(listener.selector);
            for (var j = 0, jLen = elements.length, element; j < jLen; j++) {
                element = elements[j];

                // Make sure the callback isn't invoked with the 
                // same element more than once
                if (!element.ready) {
                    element.ready = true;
                    // Invoke the callback with the element
                    listener.fn.call(element, element);
                }
            }
        }
    }

    // Expose `ready`
    win.ready = ready;

})(this);

var url = window.location.href.split("/web")[0] + "/emby";
    ready("#upTime", (element) => {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url + '/GetSystemUptimeData');

        // Track the state changes of the request.
        xhr.onreadystatechange = function() {

            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    var json = JSON.parse(xhr.responseText);
                    element.innerHTML = 'Up Time: ' + json['UpTimeDays'] + ' Days ' + json['UpTimeHours'] + ' Hours'
                } else {
                    console.log('Error: ' + xhr.status); // An error occurred during the request.
                }
            }
        };

       
        xhr.send();
    })


That is the entire updated code to place inside the script tags in the index.html.

 

This will make an assumption based on the browser window what the URL is going to be to access the plugin endpoint.

Edited by chef
Link to comment
Share on other sites

@@chef It seems to me that this is overwritten every time there's a server update... am I right?

Yes, but Im working on a way to circumvent that from happening.

Link to comment
Share on other sites

Jdiesel

Thanks @@chef

 

Nice to see you and others working on basic features that us users have been asking for for a while now. Disk space and uptime will be helpful for those who manage their server remotely and don't want another service open to internet to monitor important information.

  • Like 1
Link to comment
Share on other sites

@@Dibbes you are using a 24 hour clock.

 

I should have thought of that. That plugin is not parsing time spans in 24 hour periods, only in 12 hour spans.

 

It will now. :)

Edited by chef
Link to comment
Share on other sites

The programdata/plugin folder is untouched during updates, and the plugin folder inside of 'system' is also untouched. That system folder is right beside the dashboard-ui folder.

So it might be possible to access that folder with a couple '../../' from a script link command inside the HTML.

 

The only up keep would be a quick copy paste of a script link in index.html which could point back to the untouched JavaScript file.

 

That script file would check the exsistance and recreate any elements removed during an update and hold all the functions that were written that weren't part of the Emby core webapp.

 

Or I just submit a change request of the dashboard-ui in GitHub...

Edited by chef
Link to comment
Share on other sites

Dibbes

@@Dibbes you are using a 24 hour clock.

 

I should have thought of that. That plugin is not parsing time spans in 24 hour periods, only in 12 hour spans.

 

It will now. :)

 

Yes I am... I think you'll find most of the Europeans are....

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...