Here’s a one-liner in PowerShell to get the total size of all files found with wildcard pattern of *ORIGINAL*.
[math]::round((get-childitem -path . -filter "*ORIGINAL*" -file -recurse | measure-object -property length -sum).sum / 1MB, 2)
Here’s a one-liner in PowerShell to get the total size of all files found with wildcard pattern of *ORIGINAL*.
[math]::round((get-childitem -path . -filter "*ORIGINAL*" -file -recurse | measure-object -property length -sum).sum / 1MB, 2)
Throwing some notes here for me to remember on having PHP not buffer the output of a long running process so that it provides realtime output to the browser. I tinkered with SSE options and even AJAX/jQuery, but I finally got this to somewhat work reliably. Note that these notes are tested on PHP 8.1 and Apache 2.4.
This is required to disable buffering at the server level. Make sure to have mod_rewrite enabled.
RewriteRule ^(.*)$ $1 [NS,E=no-gzip:1,E=dont-vary:1]
<?php
ob_implicit_flush(true);
ob_end_flush();
echo 'Begin', PHP_EOL;
for ($i = 0; $i < 10; $i++) {
echo $i, PHP_EOL;
ob_flush();
flush();
sleep(1);
}
echo 'Done', PHP_EOL;
A command example using passthru
<?php
ob_implicit_flush(true);
ob_end_flush();
echo '<PRE>';
echo 'BEGIN',PHP_EOL;
passthru('ping -c 5 8.8.8.8');
ob_flush();
flush();
echo 'DONE', PHP_EOL;
?>
The following port scanner C code checks approximately 65535 ports in about 15 seconds on-network.
#include <stdio.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <netdb.h> #include <sys/types.h> #include <sys/time.h> #include <sys/ioctl.h> #include <sys/wait.h> #define MAX_PORTS 65535 #define MAX_SOCKETS 1023 // gcc -o portscanner portscanner.c int scan_port(const char* host, int port) { struct sockaddr_in server; int sock; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) { perror("socket"); return -1; } server.sin_addr.s_addr = inet_addr(host); server.sin_family = AF_INET; server.sin_port = htons(port); int flags = fcntl(sock, F_GETFL, 0); fcntl(sock, F_SETFL, flags | O_NONBLOCK); if (connect(sock, (struct sockaddr *)&server, sizeof(server)) == -1) { if (errno != EINPROGRESS) { perror("connect"); return -1; } } fd_set fdset; struct timeval tv; FD_ZERO(&fdset); FD_SET(sock, &fdset); tv.tv_sec = 0; tv.tv_usec = 200000; // Timeout of 200ms int select_ret = select(sock + 1, NULL, &fdset, NULL, &tv); if (select_ret == -1) { perror("select"); return -1; } else if (select_ret == 0) { close(sock); return 0; } // Connection successful, port is open close(sock); return 1; } void scan_ports(const char* host, int start_port, int end_port) { int num_sockets = 0; int port; for (port = start_port; port <= end_port; port++) { pid_t pid = fork(); if (pid == -1) { perror("fork"); exit(EXIT_FAILURE); } else if (pid == 0) { // Child process int result = scan_port(host, port); if (result == 1) { printf("Port %d is open\n", port); } exit(EXIT_SUCCESS); } else { // Parent process num_sockets++; if (num_sockets >= MAX_SOCKETS) { wait(NULL); num_sockets--; } } } } int main(int argc, char* argv[]) { if (argc < 3 || argc > 4) { printf("Usage: %s <host> <port/start_port> [end_port]\n", argv[0]); return 1; } const char* host = argv[1]; int start_port = atoi(argv[2]); int end_port; if (argc == 4) { end_port = atoi(argv[3]); if (start_port < 1 || start_port > MAX_PORTS || end_port < 1 || end_port > MAX_PORTS || start_port > end_port) { printf("Invalid port range.\n"); return 1; } } else { end_port = start_port; if (start_port < 1 || start_port > MAX_PORTS) { printf("Invalid port.\n"); return 1; } } printf("Scanning ports %d to %d on host %s...\n", start_port, end_port, host); scan_ports(host, start_port, end_port); return 0; }
// CFLAGS="-static-libgcc -static-libstdc++ -03" // i686-w64-mingw32-gcc -o portscanner.exe portscanner_win32.c -lws2_32 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <winsock2.h> #include <ws2tcpip.h> #include <process.h> #pragma comment(lib, "ws2_32.lib") #define MAX_PORTS 65535 typedef struct { const char* host; int start_port; int end_port; } ScanParams; int scan_port(const char* host, int port) { WSADATA wsaData; SOCKET sock; struct sockaddr_in server; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { perror("WSAStartup"); return -1; } sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == INVALID_SOCKET) { perror("socket"); WSACleanup(); return -1; } server.sin_addr.s_addr = inet_addr(host); server.sin_family = AF_INET; server.sin_port = htons(port); u_long nonblocking = 1; if (ioctlsocket(sock, FIONBIO, &nonblocking) != 0) { perror("ioctlsocket"); closesocket(sock); WSACleanup(); return -1; } if (connect(sock, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) { int error = WSAGetLastError(); if (error != WSAEWOULDBLOCK) { perror("connect"); closesocket(sock); WSACleanup(); return -1; } } fd_set fdset; struct timeval tv; FD_ZERO(&fdset); FD_SET(sock, &fdset); tv.tv_sec = 0; tv.tv_usec = 200000; // Timeout of 200ms int select_ret = select(0, NULL, &fdset, NULL, &tv); if (select_ret == SOCKET_ERROR) { perror("select"); closesocket(sock); WSACleanup(); return -1; } else if (select_ret == 0) { closesocket(sock); WSACleanup(); return 0; } // Connection successful, port is open closesocket(sock); WSACleanup(); return 1; } unsigned int __stdcall scan_ports_thread(void* params) { ScanParams* scanParams = (ScanParams*)params; const char* host = scanParams->host; int start_port = scanParams->start_port; int end_port = scanParams->end_port; int port; for (port = start_port; port <= end_port; port++) { int result = scan_port(host, port); if (result == 1) { printf("Port %d is open\n", port); } } _endthreadex(0); return 0; } void scan_ports(const char* host, int start_port, int end_port, int num_threads) { int ports_per_thread = (end_port - start_port + 1) / num_threads; int remaining_ports = (end_port - start_port + 1) % num_threads; int thread; HANDLE* scanThreads = malloc(num_threads * sizeof(HANDLE)); ScanParams* scanParams = malloc(num_threads * sizeof(ScanParams)); for (thread = 0; thread < num_threads; thread++) { scanParams[thread].host = host; scanParams[thread].start_port = start_port + thread * ports_per_thread; scanParams[thread].end_port = scanParams[thread].start_port + ports_per_thread - 1; if (thread == num_threads - 1) { // Assign remaining ports to the last thread scanParams[thread].end_port += remaining_ports; } scanThreads[thread] = (HANDLE)_beginthreadex(NULL, 0, scan_ports_thread, &scanParams[thread], 0, NULL); } // Wait for all threads to finish WaitForMultipleObjects(num_threads, scanThreads, TRUE, INFINITE); free(scanThreads); free(scanParams); } int main(int argc, char* argv[]) { if (argc < 3 || argc > 5) { printf("Usage: %s <host> <port/start_port> [end_port] [num_threads]\n", argv[0]); return 1; } const char* host = argv[1]; int start_port = atoi(argv[2]); int end_port; int num_threads = 25; if (argc >= 4) { end_port = atoi(argv[3]); if (argc == 5) { num_threads = atoi(argv[4]); } } else { end_port = start_port; } if (start_port < 1 || start_port > MAX_PORTS || end_port < 1 || end_port > MAX_PORTS || start_port > end_port) { printf("Invalid port range.\n"); return 1; } printf("Scanning ports %d to %d on host %s using %d thread(s)...\n", start_port, end_port, host, num_threads); scan_ports(host, start_port, end_port, num_threads); return 0; }
// gcc bofh.c -o bofh #include <stdio.h> #include <stdlib.h> #include <time.h> #define NUM_QUOTES 10 const char* bofh_quotes[NUM_QUOTES] = { "No, my powers can only be used for good.", "It must be a hardware problem.", "I'm sorry, we don't support that feature.", "That's a PEBCAK error (Problem Exists Between Chair And Keyboard).", "Have you tried turning it off and on again?", "The BOFH Excuse Server is down.", "It works for me.", "I'm afraid I can't help you with that.", "According to the manual, it should work.", "You must have done something wrong." }; int main() { srand(time(NULL)); // seed the random number generator int random_index = rand() % NUM_QUOTES; const char* quote = bofh_quotes[random_index]; printf("%s\n", quote); return 0; }
Looking to capture some performance metrics for website from the Linux command line and eventually get it into Cacti (RRD).
Here are my scattered notes on this process. I’m not very familiar with NodeJS stuff, so I’m documenting from installation of NodeJS on Debian 11 to creating the project.
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
apt install -y nodejs
mkdir test_project cd test_project npm init
npm i puppeteer --save
This is what I needed to grab:
apt install libatk-bridge2.0-0 libatk1.0-0 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxrandr2 libxrender1 libgbm1 libxkbcommon-x11-0
This is basic idea copied and modified from something I found online on SO. It takes an argument, the website, passed.
Create index.js:
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch({ ignoreDefaultArgs: ['--no-sandbox'], }); const page = await browser.newPage(); const t1 = Date.now(); await page.goto(process.argv[2], { waitUntil: 'networkidle0'}); const diff1 = Date.now() - t1; await browser.close(); console.log(`Time: ${diff1}ms`); })();
To run it:
node app.js https://google.com
Example output:
Time: 1201ms
TODO
The basic idea is to be able to call the node app.js https://website/
and have it return a metric (milliseconds) that can be stored into an RRD and then graphed upon. Concern would be ensuring that the poller allows for script completion — I’m not sure what would happen if node can’t complete the job before the poller times out.
Some things I scoured from the internet.
curl -s -w 'Testing Website Response Time for :%{url_effective}\n\nLookup Time:\t\t%{time_namelookup}\nConnect Time:\t\t%{time_connect}\nAppCon Time:\t\t%{time_appconnect}\nRedirect Time:\t\t%{time_redirect}\nPre-transfer Time:\t%{time_pretransfer}\nStart-transfer Time:\t%{time_starttransfer}\n\nTotal Time:\t\t%{time_total}\n' -o /dev/null https://example.com/wp-json/wc/v3
Testing Website Response Time for :https://example.com/wp-json/wc/v3
Lookup Time: 0.004972
Connect Time: 0.053358
AppCon Time: 0.112053
Redirect Time: 0.000000
Pre-transfer Time: 0.112155
Start-transfer Time: 0.746088
Total Time: 0.851602
The AskAboutPHP.com has a PHP script to grab some info (not rendering, but at least some of the connection timings) and walks through how to integrate with Cacti for graphing. There are 3 parts:
Part 1: http://www.askaboutphp.com/2008/09/17/cacti-using-cacti-to-monitor-web-page-loading-part-1/
Part 2: http://www.askaboutphp.com/2008/09/19/cacti-using-cacti-to-monitor-web-page-loading-part-2/
Part 3: http://www.askaboutphp.com/2008/09/19/cacti-using-cacti-to-monitor-web-page-loading-part-3/
For modern systems, you’ll need to fix up the pageload-agent.php file to fix the line and remove deprecated and removed function eregi
to match the following (line 10):
if (!preg_match('/^https?:\/\//', $url_argv, $matches)) { $url_argv = "https://$url_argv"; }