Recent Entries 10
- pattern minor 112d agoPerl script to check disk usage and report via emailDue to lack of central configuration management I am avoiding any Perl modules which would need to be installed on each of a great many servers. Result is more code than would normally be required in order to format HTML and send email as well as make system calls which are less desirable. ``` #!/usr/bin/perl use strict; use warnings; use Sys::Hostname; use POSIX qw(uname); my (%fsSize, %fsFree, %fsPct, %overrides); my ($thresh, $fh); my $repFile = "/tmp/chkDiskResults.txt"; my $send = 0; my $hostname = hostname(); my @uname = uname(); # Determine the OS and set the 'df' command appropriately my $df; if ($uname[0] =~ 'AIX') { $df = "df -tg"; } elsif ($uname[0] =~ 'Linux') { $df = "df -h"; } # Check for an override file loading it if it exists and running # simple checks to ensure the values are valid. my $overrideFile = "/etc/override"; if (-e $overrideFile) { open($fh, ') { my @split = split /\s+/, $line; unless (!$split[1] || $split[1] !~ /^[0-9]+$/) { $overrides{$split[0]} = $split[1]; } } close($fh); } # Execute the system 'df' command ignoring anything that isn't a # real filesystem # $cols[1] => Total space in GB # $cols[3] => Free space in GB # $cols[4] => Percent used column # $cols[5] => Mounted on column foreach my $line (qx[$df |grep -E -v "(Filesystem|proc|tmpfs)"]) { my @cols = split /\s+/, $line; chop($cols[1]); chomp($cols[3]); chop($cols[4]); # set threshold based on disk size; A 1TB disk doesn't need # to alert when 100GB are available if ($cols[1] >= 800) { $thresh = 98; } elsif ($cols[1] = 400) { $thresh = 96; } elsif ($cols[1] = 200) { $thresh = 94; } elsif ($cols[1] = 100) { $thresh = 92; } else { $thresh = 90; } $fsSize{$cols[5]} = $cols[1];# . "G"; $fsFree{$cols[5]} = $cols[3];# . "G"; $fsPct{$cols[5]} = $cols[4]; } # Do the needful; override the thresholds if necessary; write # offending filesystems to /tmp/chkDiskResults.txt as HTML # since Outlook mang
- pattern minor 112d agoLet's check that domain portIntro This simple script will allow me to check for a specific opened port on a list of domains that I own. Instead of manually doing this check, I found Python a pretty good idea for such a task. After profiling my code, I found out that `def check_for_open_ports():` is really slow. It takes about 0:01:16.799242 seconds for 4 domains. I wondered if there's a good / recommended way of improving this (maybe multithreading / multiprocessing). While asking for an answer which implements one of the above two methods is forbidden here, I wouldn't mind seeing one. I know that one shall use multiprocessing when there're I/O bound tasks which makes me believe I might go with a multithreading solution. The code ``` from socket import gethostbyname, gaierror, error, socket, AF_INET, SOCK_STREAM from sys import argv, exit import re DOMAINS_FILE = argv[1] PORT = argv[2] OUTPUT_FILE = argv[3] def get_domains(): """ Return a list of domains from domains.txt """ domains = [] if len(argv) != 4: exit("Wrong number of arguments\n") try: with open(DOMAINS_FILE) as domains_file: for line in domains_file: domains.append(line.rstrip()) except IOError: exit("First argument should be a file containing domains") return domains def check_domain_format(domain): """ This function removes the beginning of a domain if it starts with: www. http:// http://www. https:// https://www. """ clear_domain = re.match(r"(https?://(?:www\.)?|www\.)(.*)", domain) if clear_domain: return clear_domain.group(2) return domain def transform_domains_to_ips(): """ Return a list of ips specific to the domains in domains.txt """ domains = get_domains() domains_ip = [] for each_domain in domains: each_domain = check_domain_format(each_domain) try: domains_ip.append(gethostbyname(each_domain)) except gaierror:
- pattern minor 112d agoBASH script to monitor subprocess and throttle it for CPU temperature controlI need to run CPU-intensive tasks on a very old machine with overheating issues. The script below will monitor temperature and pause the jobs when it gets too high, continuing them when it's back to normal. The actual commands run are, of course, not included, since they are irrelevant to the question. I am looking for hidden traps I may have set in my code (listed at the bottom), and for other things I have done incorrectly. Aside from special characters in the commands and arguments that are run, which are hand created so I can control that risk, what traps or "got-ya's" have I unknowingly set into the code? What ways are there for making this more error-proof, or better in other ways? For the timing function I know I could have used the ``` time { command ...; command ...; } ``` construct, but I was more interested in the time spent by the machine (and previously, by me in the chair) than in the CPU time involved. The Script: The code comments should explain what it does, as well as why I did some of it the way I did. ``` #!/bin/bash # Build my time reporting function function report { # Get the current time, do the math, report the results. end_time=$(date +%s); # The time used for the last run process proc_time=$(echo "$end_time"-"$start_time" | bc); echo " ******* Processing time: $(date -u -d @${proc_time} +%T)"; # The cummulative time for all processes so far run_time=$(echo "$end_time"-"$launch_time" | bc); echo " ******* Running time: $(date -u -d @${run_time} +%T)"; } # The high and low temperatures to monitor for. Processing is paused # once the high temp is reached, and will not resume again until the # low temp is reached. # My system recovers to 60°C reasonably quick (idle is around 45°C) temp_lo=60; # My system dies at about 115°C - since 100°C is normal, suggests my # sensors are not accurate, but I work with what I have. # 20°C margin allows for delay in the detection of the high temp, and # delay in
- pattern minor 112d agoNagios check to see if a certain IP appears in a trace routeI needed a test to know which firewall in out HA environment was currently active. We have a webFilter connected to our "primary" so if the firewall fails over the webfilter is taking out of path and internet access is completely open during that time. I have a bash script that will feed two IP addresses. I am aware there is no data validation. This was done to reduce processing time. Run the trace using the first IP address and see if we can grep the second from the result. If the result is a non-zero string then return and OK result else return a Critical result. The exit codes are used by the Nagios Monitoring system to determine state and actions which is why there are custom error codes. ``` #!/usr/bin/env bash # Nagios exit codes. These are used to determine state nagiosStateOK=0 nagiosStateWarning=1 nagiosStateCritical=2 nagiosStateUnknown=3 # Initialize result traceResult='' # The address that we are going to run the tracert to target="$1" # Check for this address in the trace locate="$2" traceResult=$(traceroute -n -w 2 $target | grep $locate) if [[ -n "$traceResult" ]];then echo "$locate found in path. Still running on primary" exit $nagiosStateOK else echo "$locate not found in path. Probably failed over" exit $nagiosStateCritical fi ```
- pattern minor 112d agoProcess monitoring scriptI wrote a script which logs memory usage of a process (pid in file), and reacts in case a limit is reached. I run the script nightly. In my case, the process concerned propably has a memory leak and locks up the whole system after a couple of days. Hence, the script should reboot the system if excess memory is used. Logged memory usage values are formated to have 1 decimal place. The code works, but I'd like to know if and how I could optimize it, and if there are no mistakes. ``` #!/bin/sh # process_monitor.sh # # monitor specific process # customized for memory usage monitor (can easily be changed) # customized to perform system reboot if limit is exceeded (can easily be changed) # by geohei # created : 14.10.2016 # revised : n/a # # use cron to trigger (e.g. nightly at 04:15 '15 4 * * * ~/process_monitor.sh' # /bin/bc is used (possibly not installed by default) # # $pidfile must include path # $logpath is used to save 'process_monitor.log' file (omit trailing '/') # $pidlimit can be floating point (e.g. 14.1) pidfile='/tmp/process.pid' logpath='~' pidlimit=20.0 pidvalue=$(ps -p $(eval "cat $echo $pidfile") -o %mem --no-headers) if [ "$(echo "${pidvalue} > ${pidlimit}" | bc)" -eq 1 ]; then # log $pidvalue, $pidlimit and reboot system echo "$(date '+%Y-%m-%d %H:%M:%S') pidvalue: $pidvalue, pidlimit: $(echo $pidlimit | awk '{printf "%.1f\n", int($1)}') - reboot" >> $logpath/process_monitor.log shutdown -r -t 10 exit 1 else # log $pidvalue, $pidlimit echo "$(date '+%Y-%m-%d %H:%M:%S') pidvalue: $pidvalue, pidlimit: $pidlimit" >> $logpath/process_monitor.log fi exit 0 ``` That's how the log looks like: ``` ... 2016-10-14 04:15:04 pidvalue: 19.2, pidlimit: 20.0 2016-10-15 04:15:03 pidvalue: 20.4, pidlimit: 20.0 - reboot ... ``` Any remarks, comments or suggestions?
- pattern minor 112d agoCPU core temperature Unity indicatorProject now on Github: https://github.com/IanCaio/TempI I just finished a working version of a small Unity indicator in C, that is supposed to take the output of the application "sensors" (`sudo apt-get install sensors` required) and uses it to monitor the temperature of the CPU. It creates one indicator that has a small menu, with the name and authorship and a "Quit" button. Then it creates one indicator for each CPU core and displays the temperature of each, also changing the color of the icon depending on the temperature range. I had to make some harder than average string manipulations and have to say I'm still not comfortable with it. Maybe I just have "buffer overflowphobia". Or maybe I just need to read `string.h` documentation until I know every function without needing to type `man` every time. I tried to structure the program to make it really organized. Tried to divide it in many functions so it would be easier to review single tasks and if they are being done properly. Both code blocks are a little large for a full review, but if you spot something that is bad practice on C I'd love to hear some feedback. Even if it's related only to a piece of the code. I'll review it myself since it just got of the oven and might have some hidden bugs (couldn't trace memory leaks on `valgrind` after some point because `gtk` and `valgrind` don't get along..). Header: ``` #define TEMPI_MAX_CORES 4 #define TEMPI_MAX_CHARS 4096 #define TEMPI_COOL 50 //Function Prototypes void TempI_Free_Everything(); //Frees memory int TempI_Resolve_Executable_Path(); //Resolves executable path int TempI_Read_Config(); //Reads config file and sets the variables on TempI_Main void TempI_Set_Main_Indicator(); //Sets the main gtk app indicator void TempI_Set_Core_Indicator(); //Sets the cores gtk app indicators void TempI_Callback_Quit(); //Callback to quit program int TempI_Number_Of_Cores(); //Returns the number of cores gint TempI_Update(); //Updates the indicator int TempI_Get_Cor
- pattern minor 112d agoMultithreaded C program to calculate CPU usage of cgroupsI am writing a program in an environment that makes use of cgroups to identify and group processes together. I want to parse the CPU utilization of each cgroup by sampling `/sys/fs/cgroup/cpuacct/.../cpuacct.stat` over a 1 second interval a few times and then averaging them. It also calculates the overall CPU utilization of the system by sampling `/proc/stat` in the same manner. I want to do this in a multithreaded way because if I did it serially, the samples would happen serially, and the program would take a while to run. I also use PCRE to distinguish between two different process types. ``` #include #include #include #include #include #include #include #include #include #include #define ITERATIONS 3 #if !defined(CLK_TIME_PER_SECOND) && defined(_SC_CLK_TCK) # define CLK_TIME_PER_SECOND ((int) sysconf (_SC_CLK_TCK)) #endif #if !defined(CLK_TIME_PER_SECOND) && defined(CLK_TCK) # define CLK_TIME_PER_SECOND ((int) CLK_TCK) #endif #if !defined(CLK_TIME_PER_SECOND) && defined(CLOCKS_PER_SEC) # define CLK_TIME_PER_SECOND ((int) CLOCKS_PER_SEC) #endif #if !defined(CLK_TIME_PER_SECOND) # define CLK_TIME_PER_SECOND 100 #endif struct job_struct { char procid[50]; //store job information as procid,procType tuple char procType[50]; }; typedef struct { char *procid; //for passing to the threads char *procType; } sample_struct; int sumFlag, clk; //TODO: localize? long double sum1, sum2, systemUtilization, otherCPU; char *aStrRegex; const char *pcreErrorStr; int pcreErrorOffset, pcreExecRet; int subStrVec[30]; pcre *reCompiled; pcre_extra *pcreExtra; pthread_mutex_t total_lock = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t ok_to_add = PTHREAD_COND_INITIALIZER; int sampleCGroup(char procid[], char procType[]); void *addSampletoTotal(); void *overallUtilization(); long double getSystemUtilization(void); int getOSVersion(void); int getCPUCount(void); int main(void) { int cpus, i, jobCount, osVersion; FILE *fp; char buff[1024];
- pattern minor 112d agoCheck if the website is upapi.js ``` 'use strict'; const http = require('http'); module.exports = url => { const options = { host: url, method: 'HEAD', path: '/' }; const req = http.request(options); req.end(); const promise = new Promise((resolve, reject) => { let connected = false; req.on('response', res => { connected = res.statusCode { reject(err); }); }); return promise; }; ``` The most confusing part for me is which status codes should be considered a valid candidate? apart from that any comments regarding code are welcome.
- pattern minor 112d agoChecking the status of a website by hitting a PHP scriptI currently have a system which loops through a bunch of my website domains and checks their status and sends me a notification letting me know if they are up or down. The stripped down version of the relay code is below, I've removed the email/DB update code etc. It pings a transponder (a file on the client website server) which returns 'SUCCESS' or whatever the error it has encountered and returns that as a string. ``` function checkStatus($domain){ $statusArray = array(); $url = $domain . '/transponder.php'; $statusArray['transponder_response'] = file_get_contents($url); if($statusArray['transponder_response'] == 'SUCCESS'){ //all is well }else{ //all is not well $statusArray['transponder_code'] = get_headers($url, 1); //send code and response via email to admin } return $statusArray; } ``` The transponder code is such: ``` $dbInfo['host'] = ''; $dbInfo['dbname'] = ''; $dbInfo['user'] = ''; $dbInfo['pass'] = ''; $link = mysqli_connect($dbInfo['host'], $dbInfo['user'], $dbInfo['pass'], $dbInfo['dbname']); if(mysqli_connect_errno()){ printf('Connect failed: %s\n', mysqli_connect_error()); exit(); } if($result = mysqli_query($link, 'SELECT 1')){ if(mysqli_num_rows($result)) echo 'SUCCESS'; mysqli_free_result($result); } mysqli_close($link); ``` A 'SUCCESS' response means that the site domain is resolving, PHP is working and DB is working. Can anyone point me in a direction to make this more accurate and effective for site monitoring?
- pattern minor 112d agoThread to send heartbeat UDP packetsThis C code will run on an embedded machine with a Linux OS. It should create data packets (ASCII) to repeatedly be sent to a UDP server. Just to give an overview about what functions should do: ``` int heartbeat_processopts(struct secs *hbsec) ``` Reads a settings file and fill a data structure with some options/configurations. ``` int heartbeat_init() ``` Rusn the thread that will keep creating the UDP data packets and sending them to the server. ``` void *heartbeat_thread(void *dummy) ``` Runs the code. I think all other functions are easy to comprehend, or I hope so. This code works, but I feel that it isn't flexible. For example, if I wanted to add another heartbeat packet type I would have to write a lot of new code. I would like probably to change the `create_frame_netStatus()` function to be more reusable. heartbeat.h: ``` /* * heartbeat.h * * Created on: Jan 8, 2016 * Author: joaof */ #ifndef SRC_NT_TELEM_PROJECT_MODULES_XTRAPOLIS_DATAMANAGER_HEARTBEAT_H_ #define SRC_NT_TELEM_PROJECT_MODULES_XTRAPOLIS_DATAMANAGER_HEARTBEAT_H_ #include #include // Structure with the heartbeat values from settings struct heartBeat_values_s { char name[ 128]; // Heartbeat name int beatRate; // Frequency the heartbeat is sent to shore char remoteIp[ 16]; // Server IP int remotePort; // Server port char udpPacket[1500]; int sockfd; struct sockaddr_in sockaddr; unsigned int sockaddr_size; int counter; }; struct heartBeat_values_s *heartbeat; int nHeartbeat; int heartbeat_init(); int heartbeat_processopts(struct secs *hbsec); void *heartbeat_thread(void *dummy); #endif /* SRC_NT_TELEM_PROJECT_MODULES_XTRAPOLIS_DATAMANAGER_HEARTBEAT_H_ */ ``` heartbeat.c: ``` /* * heartbeat.c * * Created on: Jan 8, 2016 * Author: joaof */ #include #include #include #include #include #include #include #include #include #include #include #include #include #