💾 Archived View for alextheuxguy.mooo.com › 2023-06-16.gmi captured on 2023-07-22 at 16:28:07. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

Setting up a daily visitors counter on Gemini

Go back

I recently added a daily visitors count to this Capsule and wanted to share how I did it! The count updates every 15 minutes, and displays unique Gemini and HTTP visitors to my server.

Environment

This tutorial assumes the following:

Overview

I accomplish my visitor count by running a script that counts unique, daily IP addresses in my nginx and agate logs. The script then takes these counts and value swaps them in a template GemFile, and outputs that to my agate content directory. This script runs on a 15 minute cron job.

Agate setup

To get agate outputting to a log file, I run agate with the following command. You can place this command in a 'start_agate.sh' file.

#!/bin/bash
/home/USERNAME/gemini/bin/agate --content /AGATE_CONTENT --addr [::]:1965 --addr 0.0.0.0:1965 --hostname HOSTNAME --lang en-US --log-ip &>> /var/log/agate.log

You should of course replace USERNAME, AGATE_CONTENT and HOSTNAME with your appropriate values.

Nginx setup

Nginx should already be good to go, but you can verifying by running 'tail /var/logs/nginx/access.log' and confirming that you get output.

Visitor count script

Create a directory in your home folder with:

mkdir ~/visitor_count
cd ~/visitor_count

Create a new file:

vim script.sh

Paste the following code

#!/bin/bash

# Tell the server hostname and today's date
echo "Visitors to $(hostname) on $(date +%d/%b/%Y)"
# Search nginx log file for today's date and slice it up to get just IP addresses, then sort them, then get just the unique ones, then count them
HTTPV=$(grep "\[$(date +%d/%b/%Y)" $(ls -t /var/log/nginx/access.log* | head -1 ) |  cut -d" " -f1 | sort | uniq | wc -l)
# same for gopher
# GOPHERV=$(grep "\[`date +%d/%b/%Y`" /var/log/gopher |  cut -d" " -f1 | sort | uniq | wc -l)
# same for gemini
GEMINIV=$(grep $(date +%Y-%m-%d) /var/log/agate.log | cut -d" " -f6 | sort | uniq | wc -l)



UPDATED=$(date) TODAY=$(date +"%a %b %d, %Y") HTTPV=$HTTPV GEMINIV=$GEMINIV envsubst < /home/devalexwhite/visitors_sh/template.gmi > /mnt/capsule/visitors.gmi

Shoutout to M0YNG for the original script (I just modified it a bit)

Exit and save the file (escape then ':wq' in VIM). Now, make the file executable with:

sudo chmod +x script.sh

Now to create a template file. Our script loads the template, data swaps the date, HTTP visitor count and Gemini vistor count values, then outputs into our Agate content directory.

Create the file with "vim template.gmi" and paste the following in:

# Daily Visitors for ${TODAY}
=>../ Go up a directory

Last updated on ${UPDATED}

## HTTP - ${HTTPV}

## Gemini - ${GEMINIV}

You should now be able to run your script with "./script.sh" and it will output a "visitors.gmi" GemFile in your Agate content directory! Last step is to automate this process!

Automation

We'll automate running our visitors script by setting up a cron job. We'll setup a 15 minute job, but feel free to change it to whatever interval you want.

Open your crontab file with "sudo vim /etc/crontab". Add the following line at the end of the file:


Conclusion

Great, now the script should run every 15 minutes! All you need to do is link to the visitor GemFile from somewhere in your Capsule. I put a link on my homepage with:

=> /visitors.gmi 📈 Daily visitor count

If you have any questions, feel free to reach out to me!

Updates

2023-06-18 Accounted for nginx log rotation, fixed grep issues, enabled agate IP logging

---

Enjoyed this article? Reach out!

Email me