Server Stats on Desktop

Server Stats on Desktop

Knowing how my server is preforming at all times kind of an hobby with mine. I know I can subscribe to some tools and services that display this info in beautiful graphics and chars, but I like simple things, and I like using default stuff. Years ago I wrote about a similar script to this, that used this info with jQuery gauges and Panic StatusBoard, but this is a much cleaner and easier method, and much simpler, and I just like it a whole heck of a lot better. So to do this, we have three steps:

  1. Remote Server cronjob that puts top output into a non-public server_top.txt file
  2. Remote Server public PHP file server_top.php that regex’s and sorts out the server_top.txt values and creates a serialized array of our desired stats for public
  3. Finally a local PHP file geeklet-server_top.php file that converts theserialized array into something and put it on our desktop with GeekTool

First on Remote Server we need to make sure top outputs all CPU’s so we can factor them all in and find an average. To do that, run top and hit the key 1 this will reveal all CPUs. Next hit W to save this CPU-revealed configuration as default.

We then setup a cronjob with crontab -e that puts the top result in a file in a non-public directory:

Second, with our server outputing top into a non-public file, we don’t want to share all the info, so we’ll be cryptic with this next script. We’ll make a file in the public directory called server_top.php and we’ll read the .txt file, and only output a vauge array of non-compromising data, which we’ll interperut locally in the next step:

Lastly, with out Remote Server now giving us an array of the CPU, Memeory, Average Load, Top Time, and HDD size, we’ll render this data on our local server into bars for GeekTool:

We locally, now have a script creating our server stats into bars. We then create a new Shell Geeklet and run the command:

Then set the font to somthing monospace for lining up the text, and to refresh as often as our crontab does, and that’s all!

You’ll notice there’s a warning point, which simply changes the progress bar to somthing more attention grabbing if any of the stats are above a conerning percentage. If static text hidden on the desktop isn’t enough, you could take this script further and use mail() or to email you a notification of this high stat.

Use LESSCSS Client Side While Developing WordPress Theme

I write in LESSCSS because I find it’s nesting so much eaiser to read. While creating a theme, I never compile during, it’s too annoying, instead I have a on/off toggle for using .LESS file or .CSS file.

In the snippet I made the toggle WP_DEBUG, which should be on while you develop, but if you’re developing in a network it’s eaiser to use a global veriable from functions.php

Furthermore if you are using LESSCSS and are developing/maintaining a lot of WordPress themes, I highly suggest making a shortcut for LESSCSS in bash:

So all you have to do is type less when you’re in the theme directory. If you’re not comfortable with terminal, I’ve written about a PHP LESSCSS Compiler.

PreLoad Images With jQuery

Another incredible snippet if you’ve got a JS app that changes images and want said images to be ready and loaded, but not overwhelm the normal page load.

Remove Select POST FORMATS from Child Theme

The documentation left this out, but if you want to remove some post formats from a WordPress child theme, the remove_theme_support( 'post-formats') does not allow you to pass an array of formats, it’s just a hard switch. However you can over-write the original add_theme_support('post-formats') with only formats you desire.

WordPress ShortCode API – How to make a function echo and not break

According to my previous understanding and WordPress’s documentation:

Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results

So, we’re limtied, shortcode functions must return, not echo. So:

Has been my understanding for years. Unless…

Yeah. Yeah. That’s right! You can use output buffering to collect your echo’ed HTML from printing, and return it instead.

This is good to use when a shortcode has dozens of lines some multi-language script. For example I’m working on a custom Google Map with Javascript, it’s 200 lines, called to via [map] and has lots of PHP, HTML, some CSS, and WordPress conditional statements weaved into it. It’s a nightmare putting that much Javascript and multi-language markup into a PHP string, escaping quotes alone is a pile of trouble. What’s worse is the syntax highlighting from the IDE is lost, so any bug fixes or changes, you loose a great assistant. I know there’s other solutions, I could build a class, or I could do a file_get_contents() and reduce my PHP to being at the top of the script, or similar, but when the scripts not too long, it’s just so nice to have raw HTML inside the function, instead of dealing with everything else.


Update: Another great use for this is if you want to include() a local file’s echo‘ed results into a variable. If you use get_file_contents() you’ll receive the unexecuted code, storing executed code in an object allows you to return it into a variable. For the following example /file.php is just a simple file with echo "Rock the boat";

With the above, $files_output will be Rock the boat. Neat eh!

Fix for Finder

It may have to do with having Duet installed, but macOS Sierra’s Finder, Expose, and Dock – along with custom mac OS keyboard shortcuts, have been failing a bit more regularily than I’m willing to tolerate.

The fix is simple enough, just restarting them, but I have to dig through my notes or Google and find the commands I’m looking for, which interupts my workflow even more. So I made a single word Terminal alias that fixes prettymuch any blow up any of those services experience:

I added that too my bash profile via vi ~/.bash_profile and reload it with source ~/.bash_profile So now, any problem I have, just pop to Terminal (which is open all day anyways) and > fix the problem.

Simple RSA Passwordless SSH Into Unix Server via OSX

Passwordless SSH is something I use daily, but it was one of those tasks I never invested time into understanding. A one-off task: follow tutorials and forget. However, it seems every few years something changes and I have to redo my RSA keys on my Mac andor my servers.

This year, after an upgrade to Mac OS X Sierra, marks the fourth time I’ve had to temporarily learn how to set RSA Keys up again, and I’ve decided to actually invest in it this time in learning so the firth time, I can do it without reference and tons of reading.

There are many better written articles on how to do this, however, I personally found the majority of them difficult to understand or follow – so I’ve tried to make this as simple as possible.

For this tutorials “Remote” is Bash via Ubuntu 16 on EC2 AWS service, and “Local” is  Terminal on OS X Sierra.


Remote

  1. Create a key pair, where keyname is your servers name
  1. Copy the contents of newly created file ~/.ssh/keyname for next step

Local

  1. Create and paste contents from the above into this new .pem file (close and save file). Then add the .pem to OSX

That’s it. Really.


You can now login passwordless:

I have three optional extra’s that I personally do that make it a litter better. First, name your keypair appropriately, most tutorials all have idrsa or idusername and if you’ve got or will get multiple servers, your local ~/.ssh folder will be a pile of unknown randomness.

I also suggest setting AWS security to allow SSH only on a random port, like 42111 or something like that, prevent unwanted SSH attempts by limiting where your door is.

Lastly, adding an alias to your local bash for the SSH connection command with a single word is a great second saver. Heres how to do it via Terminal on your Local:

Random Image From Select Flickr Groups via PHP

Yesterday I wrote about Cache’ing CURL requests, today I had to write a small script that uses it for a web app I’m working on. It uses serialized PHP data instead of JSON but everything’s pretty much the same.

The app I’m creating needed a dynamic background image, something professional, new, and interesting. I wanted a pool of images (so each request has a new image) but I didn’t want the pool to be too big. I also wanted the pool of images (Flickr Groups) to change on once in a while, so it wasn’t all too similar (landscapes, or all portraits, etc.).

The script below outputs a random image from a stream of 20 images from a Flickr Group (though it can be set to output just one image per $refresh). The Flickr Group is random too and comes from a predefined array of Groups I selected.

The above will output

Where {group_name} will change every $refresh seconds, and {image_url} will be 1 of 20 images on rotation every request and change with the $refresh group change.

To get the Groups ID’s I used idgettr.com and I got the Flickr API Key $flickrapi from Flickr API.