Sponsored Content
Top Forums Web Development Creating a Simple Linux Dashboard with Oracle Jet Post 303024973 by Neo on Monday 22nd of October 2018 03:09:34 AM
Old 10-22-2018
Creating a Simple Linux Dashboard with Oracle Jet

Creating a Simple Linux Dashboard with Oracle Jet - Part 1 the Server Side PHP Code

Creating a simple Linux dashboard with Oracle Jet is easy and fun. It's simple to create a dashboard to monitor your Linux server using Oracle JET. The sky is the limit with indicators and gauges.

Here is some very basic server side PHP code to build an Oracle JET dashboard. The code is not fully optimized, but should help anyone who wants to build a dashboard.

Code:
<?php

// anyway to start you request is fine.  In this script the $_GET request is not required.
// basically we build a JSON string from a number of simple Linux indicators:

if ($_GET['loadavg'] == '1') {
    $out = '{';
    $out .= getLoads();
    $out .= getLEDIndicator('mysqldump');
    $out .= getLEDIndicator('gzip');
    $out .= getLEDIndicator('find');
    $out .= getLEDIndicator('rsync');
    $out .= getdiskPercent('md2');
    $out .= getMem('MemFree');
    $out .= getMem('Cached');
    $out .= getprocessCPU('all');
    $out .= getprocessCPU('apache2');
    $out .= getprocessCPU('mysqld');
    $out .= getprocessCounts('apache2');
    $out .= getprocessCounts('rsync');
    $out .= '}';

    // on our server we have a  can_moderate() function that controls access to the JSON file
    // however, you can use any access control method you like:

    if (can_moderate()) {
        header('Access-Control-Allow-Origin: *');
        echo $out;
    }

}

// the functions are simple, and self explanatory and could be further combined
// but you get this idea:

function getprocessCounts($get = 'rsync')
{
    $target = 'ps aux | grep ' . $get . ' | grep -v grep | wc -l';
    $target_results = shell_exec($target);
    $output = ',"' . $get . 'count":"' . trim($target_results) . '"';

    return $output;
}

function getLoads()
{
    $string = file_get_contents('/proc/loadavg');
    $loadavg = explode(' ', $string);

    $n = 0;
    for ($n = 0; $n < count($loadavg); $n++) {
        $key = 'load' . $n;
        if ($n < count($loadavg) - 1) {
            $output .= '"' . $key . '":"' . trim($loadavg[$n]) . '",';
        } else {
            $output .= '"' . $key . '":"' . trim($loadavg[$n]) . '"';
        }

    }

    return $output;
}

function getLEDIndicator($get = 'apache2')
{
    $cmd = 'ps aux | grep ' . $get . ' | grep -v grep | wc -l';
    $cmd_results = shell_exec($cmd);
    $output = ',"' . $get . '":"' . trim($cmd_results) . '"';
    return $output;
}

function getdiskPercent($get = 'md2')
{
    $df = 'df | grep ' . $get . ' | grep -v grep';
    $df_results = shell_exec($df);
    $usage = preg_split('/\s+/', $df_results);
    $percent = explode('%', $usage[4]);
    $output = ',"' . $get . '":"' . trim($percent[0]) . '"';
    return $output;
}

function getMem($get = 'MemFree')
{
    $getMem = 'cat /proc/meminfo | grep ' . $get;
    $getMem_results = shell_exec($getMem);
    $mem = preg_split('/\s+/', $getMem_results);
    $output = ',"' . strtolower($get) . '":"' . trim($mem[1]) . '"';
    return $output;
}

function getprocessCPU($get = 'mysqld')
{
    $sum = 0;
    if ($get == 'all') {
        $doit = 'ps -eopcpu,comm';
    } else {
        $doit = 'ps -eopcpu,comm | grep ' . $get;
    }

    $results = shell_exec($doit);
    if ($get == 'all') {
        $clean = explode(' ', $results);
    } else {
        $clean = explode($get, $results);
    }

    foreach ($clean as $line) {

        if (s_has_letters(trim($line)) or trim($line) == '') {
            continue;}
        {
            if (strlen($line) > 0) {
                $sum += trim($line);
            }

        }
    }
    $output = ',"' . strtolower($get) . '":"' . trim($sum) . '"';
    return $output;
}

function s_has_letters($string)
{
    return preg_match('/[a-zA-Z]/', $string);
}

Here is some sample JSON output. Please note in a future update to this code I will update the loadavg names in the JSON to more accurately describe the indicators:

Code:
{
  "load0": "0.69",
  "load1": "0.57",
  "load2": "0.56",
  "load3": "1/291",
  "load4": "29704",
  "mysqldump": "0",
  "gzip": "0",
  "find": "0",
  "rsync": "0",
  "md2": "18",
  "memfree": "54970768",
  "cached": "5185620",
  "all": "39.5",
  "apache2": "35.8",
  "mysqld": "3.6",
  "apache2count": "93",
  "rsynccount": "0"
}

In the next post; I'll post the Oracle JET module, including the Javascript and HTML files.
 

4 More Discussions You Might Find Interesting

1. What is on Your Mind?

Excellent Oracle JET Video - "Finally, JavaScript Is Easy!"

This is a video well worth watching if you have any interests at all in the future of web development, web development frameworks and Javascript. https://www.youtube.com/watch?v=V8mhIEeTMCc . Fixed typo in Oracle Jet URL (oraclejet.org) (0 Replies)
Discussion started by: Neo
0 Replies

2. Web Development

Oracle Jet - LP: 10. Lesson 1: Oracle JET 4.x - Lesson 1 - Part 4: Data Binding

Working on LP: 10. Lesson 1: Oracle JET 4.x - Lesson 1 - Part 4: Data Binding in this Oracle JET online course - Soar higher with Oracle JavaScript Extension Toolkit (JET), I have created this code for incidents.js I cannot get the load average data in this Oracle JET test to update the... (4 Replies)
Discussion started by: Neo
4 Replies

3. Web Development

Oracle JET 4.x - Lesson 1 - Part 9: Oracle JET Cookbook (Gauges and Ints)

Working on: 10. Lesson 1: Oracle JET 4.x - Lesson 1 - Part 9: Oracle JET Cookbook (which I highly recommend) and using the server loadavg code I wrote and have been adding gauges. All is great so far, and I'm loving JET, but have ran into an issue. Here is the loadavg.js code: /** ... (1 Reply)
Discussion started by: Neo
1 Replies

4. Web Development

A Quick Web Developers Review of Oracle JET

Oracle JET is marketed as a kind of "anti-framework" approach to web development but from my experience Oracle JET is just another type of framework. So, I would describe JET as "a meta-framework" because JET is a framework that is built to import and use other frameworks and Javascript... (4 Replies)
Discussion started by: Neo
4 Replies
All times are GMT -4. The time now is 02:02 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy