#php

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 charts, 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:

https://gist.github.com/davidsword/5020318fe00c167a1d00107fd8182c5e

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:

https://gist.github.com/davidsword/b01a38ea335471c23615e0330eeeb286

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:

https://gist.github.com/davidsword/9efeecac8769887a2a2afb038011153a

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

https://gist.github.com/davidsword/a382521e4d04073a9b15f9c24c69a952

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.

https://gist.github.com/davidsword/a3760b1ae2e00802c677ea2a66132f08

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:

https://gist.github.com/davidsword/71e290a3afd296e841bca3f33099d5e2

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.

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.

https://gist.github.com/davidsword/a9087efdd6525f5217ffeaab2154111c

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:

https://gist.github.com/davidsword/394108430a76c36aa9cefb6c809dc187

Has been my understanding for years. Unless…

https://gist.github.com/davidsword/1a590c2a11bb55531ddfc98eb83afee3

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";

https://gist.github.com/davidsword/29aa612b73266c72b79e41bddbe01174

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

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.

https://gist.github.com/davidsword/7c32c7d2d77928eeac9ecf629167b7fd

The above will output

https://gist.github.com/davidsword/e091b78457f812571745e711203f1fbe

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.

Really Simple PHP CURL and Cache of JSON Result

When using PHP CURL with a third party API, I’ve found keeping the JSON results in a local cache is easier than constantly querying the service – especially when the API is slow or has query restrictions.

Here’s a great snippet I save in my tool belt and use quite often:

https://gist.github.com/davidsword/e89955cbc439dc3a8856c64d56954094

If the API is really slow (I’ve encountered two myself) I suggest splitting the snippets CURL+Cache section and putting it into it’s own file, from there create a CRON command to execute the independent CURL+Cache file. That’ll separate the slow API from your PHP app, and your JSON data will still be up to date in the background.

Emoji short name’s to HTML HEX Code via PHP

I’ve been working on a Slack App that shows recent Slack posts and activities. The majority of recent posts include emojis, and problematically Slack’s API doesn’t store and send emojis as characters, they’re down-converted to their respective shortnames, then wrapped in colons (presumably for universal support – it appears to be a standard practice of some sort, as many use it).

Basically I was just looking for converting

:thumbsup: to convert to: 👍 which’d show: 👍

Simple enough task, but I couldn’t find any resources to do it (and if you’ve discovered this blog post, you’ve probably discovered that out too). So, starting at the top, Slack in their API docs refers to using github.com/iamcal/emoji-data as their emoji shortcode source. This repository does have a PHP script, but it doesn’t convert as simply as I wanted it to, and it’s huge.

Taking a step back, iamcal/emoji-data has a enormous master .json file, basically a database of each emoji and all variations therein. Taking this file, and running through it with some basic PHP, we can build a simple array of shortcode => hexcode. Doing this was almost too easy:

https://gist.github.com/davidsword/74ed32b5e6bf475ce847b5da88d5d89b

We then take the output of that file, and create our own emoji_unicode.php file, an array housing the values output above. Which looks like:

https://gist.github.com/davidsword/40de8686bb693ad0e4b742d1df32aad2

Much smaller and easier to work with. 97% smaller actually coming in at: 35KB instead of 928KB. I’ve hosted the file on github here.

So with a simple array to call back to, we can just include it, search the string for the colon’ized shortcode emojis and replace with hex code, like so:

https://gist.github.com/davidsword/657de8877eb3bff3f5fc31e2a439c134

Which’d output

> hello 🌎 . it’s 🔨 ⏱.

This works with :skin-tone-x which surprised me- I guess the skin-tone is a “invisible” character of it’s own, that basically gets compounded with the emoji character before it, and changes the emojis that way. Pretty cool, so no extra work for that. (And no, this method doesn’t work on devices that don’t support emojis).

Kindle Currently Reading on Panic Status Board or Website

Kindle Currently Reading on Panic Status Board or Website

Here’s a neat little widget that’ll fetch your currently-reading and read books from your Goodreads shelf, and show them as a widget for either Panic Status Board (DIY Panel), or your website. Code is inspired and built upon from a great little 2013 script from flapane.com

First, some styling:

https://gist.github.com/davidsword/e909b447c550dc07231720880752a82a

Setup, query and cache:

https://gist.github.com/davidsword/af54a40674b53c37b84e364adc29c3cc

Read and output:

https://gist.github.com/davidsword/6920a66855e522da8f3af3d00a671406

You’ll need to create empty file goodreads.cache manually, in the dir of the script.

I’m not entirely sure what happens when there’s more than 1 currently-reading. I never bothered checking.

$image_fix is nasty, I don’t understand why the Goodreads user API would not return a cover when the cover is present on the Goodreads site itself. I had to do a fix for one of my books, manually tracking down the cover’s URL on Goodreads.com.

<!--?= (98/147)*54 ?--> is my ..interesting.. way of doing proportional scaling on the fly. Old width/height values kept prior to overwrites for reference; makes sizing your widgets books covers to much easier.

filter: grayscale(100%); Keepin’ it real with Kindle, grayscale the covers.