Node-RED, npm, sudo and permissions

I ran into a problem on my Pi yesterday; I could not update any npm installed packages.  If I did try, it simply failed, quietly.  Some searching led me to believe that it was because I had installed npm using sudo and possibly other packages as well (I’d lost some Bash history so I cannot be sure – a blog for another time).

I dug around a little and found this comment on StackOverflow;

“Recent versions of npm switch to user “nobody” as a security measure if it’s being run with sudo privileges.”

I could see exactly this within /usr/lib/node_modules.

On the npm site itself, there is an item on this issue which suggests some modifications which I really did not look the like of, as  npm config get prefix  returned just /usr.

Another item I found suggested changing permissions in a similar manner to the npm docs but I felt it was rather over the top and changed too much.  So I took a middle road and did the following;

This changed all the ‘nobody’ owned files and folders to owned by pi (which I was logged in as).

What I cannot remember (lost history again) is if I did it for the folders in my home folder. If I had found any files owned by ‘nobody’ I would have done so.

This has solved the problem and now I can use npm without sudo and update (both locally and globally) work fine.

HTH

Facebooktwitterby feather
Posted in Linux, Node-RED | Tagged , | Leave a comment

A New Discovery

I actually love it when I come across something new, even if I should have found it ages ago. WinSCP. How I have never come across it beats me, but if you regularly edit files on different systems, this is for you. It even imports PuTTY session information.

Facebooktwitterby feather
Posted in RaspberryPi, Technical - General | Leave a comment

Trouble with Hosting at SmartHost

If you happened to notice a number of posts simply disappearing, it was because my hosting provider SmartHosting had some sort of failure last week which meant they lost a number of days work.  I noticed it on Sunday and they had to go back to the last backup which was Thursday.  Rather unfortunate especially as I have only been with them for a few weeks and I just happened to do a load of updates in those couple of days.

Now I’m not unhappy it happened, these things do happen, but going back to a backup 3/4 days ago seems a little off. OK I don’t pay for the super duper backup but still…

What I am less happy about is their communication.  If I hadn’t noticed it, they would never have told me as I have received no formal notification and I would have wondered where it had all gone.  Their status page is impossible to find until support send you the link and it is rather reticent in explaining the problem and it fails to mention they went back to a relatively old backup so many people would have lost data.  Their twitter feed appears to never be read or if they do they don’t actually respond.

They claim a 99.9% up time.  It took 6.5 hours to fix, so if that is 0.1% they should not have another outage for 270 days.  Let’s hope so or else I have paid for a pup.

Facebooktwitterby feather
Posted in General | Tagged , | Leave a comment

Node-RED and XML to JSON

I’m trying to catch up with a whole load of stuff I did before I started this blog; as much as anything, so I can remember what I did for when I need to do it again!

Following on from installing Monit, I wanted to use Node-RED to monitor Monit so that ultimately I can have a dashboard for all sorts of things to be monitored and possibly alerts (rather than Monit doing that directly).

As previously mentioned, Monit communicates via an http interface so extracting the data was remarkably simple. All you need is a websocket flow and a URL like http://x.x.x.x:2812/_status?format=xml . It seemed that the obvious way to use this data was to convert the XML to JSON.

However, when I tried the built-in XML parser it did not work. Some digging brought up the fact that this built-in node has a limited depth. Not very useful. Further research led me to the xml2json node on npm. I’d never really understood the relationship between npm and Node-RED. What I think I understand is that Node-RED uses some command line nodes from npm which in itself is a Javascript package manager.

So in order to use this command line node (rather than a Node-RED flow) I needed to install it then ‘require’ it. The whole Javascript ecosystem is rather alien to me at the moment so getting it installed took a fair bit of experimenting although it is actually very simple to do.

It comprises of 3 steps:

  1. Install the node from npm. As you want to use it from Node-RED, you need to *not* install it globally – counter-intuitive right? So simply go into your node red folder (usually ~/.node-red) and then npm install xml2json (reference).
  2. Next, you need to edit your settings.js file to ‘require’ the node (reference). This file is also usually found in the ~/.node-red directory.
    Look for the functionGlobalContext and add in a line for the xml2json node.

    Save and restart Node-RED sudo service nodered restart .
  3. Add the code into a function such as:

This will now enable you to take an XML flow and break it down into its constituent parts

The complete Node-RED flow:

 

Facebooktwitterby feather
Posted in Coding and Programming, Node-RED | Tagged , , , | Leave a comment

Reducing Spam on WordPress and Blacklists

It is a shame that there are folk in the world that have nothing better to do that try and irritate others. Sadly that is the way it is.  Inevitably, having only set the blog up a week ago and wanting to allow genuine users to register, I started to get unwanted registrations.

What to do about it? A quick Google identified a blog that led me to a plugin to block registrations from certain domains called Ban Hammer.  Once installed you need to manually include a list of domains.  Again quick search found a list on GitHub which was quickly copied and included.

On further digging though, I discovered that the under the settings for comments, there is a blacklist of domains.  As my new registrees email domains were on the blacklist I presume WordPress prevented them posting a comment.  Shame it did not prevent them registering in the first place.

I have suggested to the author of Ban Hammer he uses the same blacklist – I’ll see where that goes.  In the meantime I also found a plugin to keep the main blacklist up to date – presumably, without this addin, it is only updated when WordPress itself is updated. Simply called Blacklist Updater it checks for updates from GitHub twice a day.

There are some other suggestions on WPBeginner for reducing spam. Happy hunting.

Update

I had a response from the Author of Ban Hammer saying that the aim of the plugin was to stop specific trolls etc. I’ve added in all the blacklist entries and I have not had anyone register an account since, so despite not being designed for it, it still seems to work.

Facebooktwitterby feather
Posted in WordPress | Tagged , | Leave a comment

USB to VGA adapter

A really quick blog on an item I got from Amazon this week

I had a couple of old 21″ monitors kicking around that I wanted to use on a dual stand, but the monitors only have a VGA input.  I’d heard these little devices worked well so I went for the cheapest I could find. Other than not reading the instructions (it first appears as a USB drive that has the driver on it) it just works on my Win 10 laptop. It does need USB 3 (well this one does) so next is to try a USB 3 hub and see if I can run more than one at a time.

Facebooktwitterby feather
Posted in Hardware | Leave a comment

Bash on Windows

Bash on Windows is a fantastic tool. If you have not heard about it I suggest a bit of Googling but in short, it is a fully fledged (well almost) Ubuntu system running right on Windows.  I don’t intend to repeat what is already out there so a couple of resources to get you started;

What I will say is that the single thing it has made so simple is moving files between Windows machines and a Pi.  A simple rsync command and you can move as many files or directories as you need.

If you want a good guide on using rsync I found this does the trick; http://www.tecmint.com/rsync-local-remote-file-synchronization-commands/

One thing I did struggle to find, is where the Bash user’s home folder is under windows; for reference, it is found here:

The lxss folder is a hidden one so you will need to change that in the Windows Explorer view settings.

Facebooktwitterby feather
Posted in Technical - General | Tagged , , | Leave a comment

Monitoring Processes using Monit

Monit and it’s big (paid for) brother M/Monit are great tools for monitoring processes on your Raspberry Pi (or any Linux based system).  The first thing to note though is that Monit cannot be used to monitor processes started using a systemd service file (as that can be set to recover it’s own daemons), rather it can just monitor daemons started using init.

However, that is not the whole story as Monit can monitor many other things, too numerous to mention, but including http services, and even if a file, that should be written regularly has been written to (I use this to monitor pywws).

It is easy to install, sudo apt-get install monit  and there are plenty of resources out there on setting Monit up.  There is one key thing to remember, all of the output basically comes from the httpd interface so this has to be enabled.  Even a sudo monit status  will fail without that being setup and it isn’t by default.  So before you do anything you need to edit the file sudo nano /etc/monit/monitrc .

In this file look for the following set httpd ; I then change the text to read as follows (X.X.X.0 being your IP range):

I run all of my Pis headless so I want to be able to connect from other machines on the local network. I don’t bother changing the usual port as I block that at a higher level and as yet have not used any username/password, but it is easily set up.

Exit the editor and restart the Monit service sudo service monit restart

The final thing you need to do is open the port on ufw sudo ufw allow 2812/tcp  and reload it sudo ufw reload; you have installed and enabled ufw, haven’t you?

Update

So I tried to follow my own advice on an OrangePi running DietPi and the lines above to enable monit access would not work.  Instead I got this error:

Interestingly I could access it from a remote host on the network via http://x.x.x.x:2812 but not in an ssh session.

The solution was to add:

to the above lines to the monitrc file and then it was happy.  Not sure why the 2 installations differed, but they did!

Facebooktwitterby feather
Posted in Linux | Tagged , | Leave a comment

WordPress Plugins and Code Snippets

I have used WordPress on and off for a number of years and I think I can say it does the job. However, it does annoy me sometimes.

With this blog, I intend to share quite a lot of code and I like code to be embedded in the text. Just find the right plugin I thought. If only it was that simple.  Firstly the Plugin search seems to generate the results at random and secondly, there is the no way of filtering or sorting them by ranking, install base, last updated etc.

So I resorted to Google, but this route had it’s own problems.  There are lots of blogs and items about this problem, but some are way out of date and more annoyingly, the blogs do not have dates on them so you don’t know they are out of date.

I had a couple of key requirements;

  1. Obviously, must highlight syntax.
  2. Provide a means to copy the code.
  3. Put the code in a scrollable box.

After much searching I settled on 2 different solutions.

Firstly I am using Crayon Syntax Highlighter for code blocks and in-line code or commands. This has an amazing number of options (almost too many) so you really can make it do what you want it to do.  That is except include code from a Gist.

For that I have settled on oEmbed Gist and some custom CSS to limit the box size.  The plugin is great in that you simply put the public URL in your text and it does the rest.  However, long code snippets are just that, long, and I wanted to limit that within a scroll box.  After yet more much searching I came across this CSS code on StackExchange which does exactly that (displayed using Crayon!).

To use it, on the site dashboard go to Appearance / Customise and then select Additional CSS. Paste in the CSS above and then save. As simple as that.

I have also installed TinyMCE Advanced which gives a few more options for general blog writing as well.

Plugin List

Finally, this task would have been so much easier if folk listed the plugins they use! You will find a list of active plugins on this site from the top menu and I’d encourage all WordPress users to do the same. It does of course use a plugin (Plugins List) to do it 🙂

All you need to do is create a new page and insert the short code [plugins_list] (if you are wondering how to display a shortcode without it being expanded you need double square brackets).  Please, do it now!

Other Plugins

Finally, to possibly save you time, a list of the other plugins I looked at:

  • Google Syntax Highlighter for WordPress
  • Code Prettify
  • Pastacode
  • Prismatic
  • TinyMCE Pre Button
  • WP-Syntax

 

Facebooktwitterby feather
Posted in WordPress | Tagged , | Leave a comment

Installing Raspbian onto the PiDrive (HDD)

In the previous blog, I explained how I got to this point. Briefly, I have a new B+ RaspberryPi and a WD Labs PiDrive HDD.  The aim is to end up with Jessie Lite booting off the PiDrive.

There are 2 key sources of information that I used.  First is one from AdaFruit and the second a script from the EmonCMS scripts repository with some documentation here.  They are largely the same.

I found the USB_HDD script in the EmonCMS repository worked well except that it fails first time. One solution suggested is to put a ‘wait’ in, but I found that simply running it a second time seems to solve the problem.  No real idea why!  I think it may be that the second time it is run, the folder is already created so it keeps going.

If I was doing it again, I would change the size of the partition so that it did not create one big partition, but rather a smaller one for the system and a larger one for data.

Failed SD and Recovering Booting off the HDD

I have just has a failure of an admittedly, recycled, SD card.  The problem was I did not have a backup of the cmdline.txt file.  If you have looked at the script, you will see it gathers some information about the HDD partition and then creates the cmdline.txt entry to boot off the SD Card.

After much fiddling I discovered the solution was quite simple (would have been simpler if I had backed up the file).

First thing is to get a new SD Card, and put the image of your choice on it. Personally a Jessie Lite fits on a 2Gb card so I go with that.  Plug the HDD in and boot the Pi.  It will of course boot into a bare system running off the SD Card.  All you need to do, is change the cmdline.txt (simple eh?).

First you need to know what the drive is called.  It is most likely to be sda1, so the command:

will produce something like:

What we are trying to do is get that value of PARTUUID into the cmdline.txt so it looks something like this;

I had some fun getting it into the file.  the only way I could find was to  sudo -s  to permanently be the super user, backup the existing file

then pipe the value of PARTUUID into the file so;

if you then edit the file  nano /boot/cmdline.txt  (or use your favourite editor) to get the line looking as above.

Once you have done that simply reboot check it all works then save the cmdline.txt file somewhere on the HDD. Once you have done that you will, of course, never need it.

On the off chance that this does not work, you can put the SD Card in another machine and copy the cmdline.txt back (so it boots off the SD Card) and then look into it further.

Facebooktwitterby feather
Posted in RaspberryPi, Technical - General | Tagged , , , , | Leave a comment