Getting current memory usage

I’ve started gathering the information that I need in order to be able to create the user interface that I want for my PI3 home server.

One of the things I want to do is write a program that will run all the time that the pi is online, it’ll be started and stopped by systemctl just like open vpn etc.

What this program will do is one a second take some vital statistics of my PI and upload it to a MySQL database.   The first part of the information that I wanted to gather is the memory usage of my machine.  In order to be able to properly calculate the ram usage, I needed to get all the different types of memory allocation.  The reason for this is the “used” memory in linux simply means memory that is being used for something, this does not mean that it’s not available.   On the linux OS  you can execute the command “free” to get the amount of memory used

 

free
             total       used       free     shared    buffers     cached
Mem:        234452     217780      16672      10564      36684     100736
-/+ buffers/cache:      80360     154092
Swap:       102396         20     102376

As you can see from the output above. you get lots of different pieces of information.  To find out the true amount of used ram, you can user this formula

 

actual used = total – (free + shared + buffers + cached)

So using the figures above.

actual used = 234452 – ( 16672 + 10564 + 36684 + 100736 )

actual used = 234452 – 164656

actual used = 69796

That’s alot less than the reported 217780 of used Memory.   The reason for this is that shared, buffers and cached memory can be dumped by the operating system to make way for other processes.  So, on my PI, (It’s a Model B 256MB ram) it that will happen sooner than on a PI2 or PI3, or even a later PI.

 

There are several ways to get at the system memory using raspbian.

 

cat /proc/meminfo
MemTotal: 234452 kB
MemFree: 16124 kB
MemAvailable: 128144 kB
Buffers: 37116 kB
Cached: 100736 kB

Shmem:             10564 kB

SwapTotal: 102396 kB
SwapFree: 102376 kB

 

It’s all there at loads more too.  however, it’s shown in kB  which means that there’s information missing, it’s rounded to the nearest kB, and being pedantic I want the exact number of bytes.

 

What about the c command sysinfo?

 

This link provides the details about sysinfo.  Upon reading, it looks promising, however there’s a fatal floor in this plan.  The cached memory isn’t reported, which kinda messes up the plan completely.

 

This really leaves me with one option that I can see so far.  The linux command “free -b”  the -b means return the values as bytes, which is perfect, and the output is similar to above.

 

free -b
             total       used       free     shared    buffers     cached
Mem:     240078848  223744000   16334848   10817536   38113280  103157760
-/+ buffers/cache:   82472960  157605888
Swap:    104853504      20480  104833024

That's the figures that I'm after, just need to get at the them now.

So, to do that here's a little c method that I wrote.

struct MemoryUsage{
 unsigned long RamTotal;
 unsigned long RamActualUsed;
 unsigned long RamUsed;
 unsigned long RamFree;
 unsigned long RamShared;
 unsigned long RamBuffers;
 unsigned long RamCached;
 unsigned long SwapTotal;
 unsigned long SwapFree;
 unsigned long SwapUsed;
};

struct MemoryUsage GetMemoryUsage()
{
 struct MemoryUsage memoryUsage;
 
 memoryUsage.RamTotal = 0;
 
 FILE *fp;
 
 fp = popen("free -b","r");
 if (fp == NULL)
 {
 perror("Error opening file");
 exit(EXIT_FAILURE);
 }
 
 char str[1024];
 int lineNo = 0;
 char token[1024];
 
 while (fgets(str, sizeof(str)-1, fp) != NULL) {
 lineNo++;
 
 if (lineNo == 2)
 {
 sscanf( str, "%s %u %u %u %u %u %u",
 token,
 &memoryUsage.RamTotal,
 &memoryUsage.RamUsed,
 &memoryUsage.RamFree,
 &memoryUsage.RamShared,
 &memoryUsage.RamBuffers,
 &memoryUsage.RamCached
 );
 }
 if (lineNo == 4)
 {
 sscanf( str, "%s %u %u %u",
 token,
 &memoryUsage.SwapTotal,
 &memoryUsage.SwapUsed,
 &memoryUsage.SwapFree
 );
 }
 }
 
 fclose(fp);

 memoryUsage.RamActualUsed = memoryUsage.RamTotal - ( memoryUsage.RamFree + memoryUsage.RamShared + memoryUsage.RamCached );
 
 return memoryUsage;
}


To call it, all you need to do is...

struct MemoryUsage memoryUsage;
 memoryUsage = GetMemoryUsage();

And at this point, you should have all the memoryinformation that you could want.  Including the actual used ram from the formula about.

Installing Apache, PHP and MySQL on a Raspberry PI

Why Apache?

I like to go into projects with my eye’s open.  There are choices about which server software to use.  On the linux platform there are two web servers.  Apache, and nginx.  Looking at the January 2016 Web Server Survey apache is the number one used web server on the internet.  It’s running the websites of about 35.59% of the internet.  nginx is running about 17.43%.  It’s up and coming, so does mean that it’s worth looking at.  However, for my needs, I’ll be going with the no.1 software at this time.  Also I’ve used Apache for other projects and have some experience with it.   Not only that, Apache has been the number one web server for the internet for nearly all of the last 20 years.  They’ve got to be doing something right for have a statistic like that.

 

Why PHP?

PHP is a scripting language. It’s the language that is used for WordPress, and other projects.  Again, I have some experience with it, it’s also quite similar to C# which is my first language, so I’m comfortable switching between the two.

 

Why MySQL?

There are choices here, MySQL, Postgress, MariaDB for starters.  Again, this is mainly a personal choice.  Pogress would be my second choice.  MariaDB is based on MySQL from what I know about this MariaDB is shadowing MySQL and is trying to improve on what MySQL has already implemented.  From the sounds of things, I get the feeling that this is some political thing. MySQL is alot easier to install than MariaDB, so I’ll stick with MySQL.

 

User interface for my mega server

A couple of years ago I started a series of posts about setting up a Lamp configuration on a Debian Virtual maching running on VMWare Player.  Of course as with many side projects, I never really got round to finishing it.

It’s time for me to fix that.  Below the break is the details of a project and the reason why I need a LAMP box

All in one server (DHCP, DNS, NTP and VPN)

In the previous posts, I’d decided to split up some of the services onto more than one PI.  Currently I have three Raspberry PI’s Employed performing functions for my network.  My Raspberry PI, is performing the DHCP, DNS and NTP roles.  My PI 2 is doing the job of a Wake on Lan server.  It’s basically a web server that can send out the magic packets.  And my PI 3, is doing the VPN stuff.

Adding a DHCP server

Next step in this master plan is to add a DHCP server to the PI that is running my DNS server and NTP server.   Doing this step means that the DNS server on my PI will become the goto DNS server for my entire network.  At the moment, the default is 192.168.1.1 (my router) for my lan computers and 192.168.1.6 (by DHCP/DNS/NTP server) for my VPN.  This is inconsistent and means that I cannot use my DNS names for computers on my LAN – which could be confusing.  By switching my DHCP over to my PI, I can get full control of the DNS part and have that routed first to my PI, then on to the internet.

Switching my VPN to use my new DNS server

This post is a quick interlude as I want to get make some progress on my VPN Server setup.

The end goal that I’m attempting to get to here is that I can use my VPN and access the machines inside my network without having to open up ports on my firewall.  The VPN will allow the access, then I want to be able to navigate around using my DNS server rather than having to type in IP Addresses.

DNS Server

This post is part of a series that starts here.  It assumes that you have already configured your Raspberry PI using the instructions in that post.

NTP Server

97tAn NTP server is a good way to synchronise the time across all the machines on a network.  This is useful for enterprise level software with many servers, so that anything date and time related will be the same.  The idea is that a computer can ask the NTP server what the time is, then set it’s own clock accordingly.

 

For reference, I’m using this post as a guide to getting the NTP stuff setup.  However, it was made two years ago and in tech terms, things change.  The change from Rasbian Wheezy, to Jessie may be enough to make the reference that I’m following out of date.

Raspberry Pi DNS, NTP, DHCP and Active Directory server

In this project, I’m going to set up my original Raspberry Pi model B to do the functions that would be done by an office network.

So what is this bad scrabble hand of letters, and what can they do for me?

VPN Server on a Raspberry PI 3

ConsoleWHY?

I want to be able to connect to my home network from anywhere.  There are two ways to do this, the first is to open up ports on my firewall, and route them through to the various programs that I want to be able to access from the outside world.  This approach has implications, firstly it means that anyone could attempt to connect to those services that I want to access.  This means that someone could hack into my network.  This isn’t a nice thought.