Skip to content

Commit

Permalink
Initial commit of work. Seems to do most of the things that I want. N…
Browse files Browse the repository at this point in the history
…eeds more work.
  • Loading branch information
seanbruno committed Jan 8, 2015
1 parent 372a3da commit 83fa0ab
Show file tree
Hide file tree
Showing 10 changed files with 289 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Makefile.client
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
PORTNAME= panicmails-client
PORTVERSION= 0.2
CATEGORIES= sysutils

RUN_DEPENDS= panicmail:${PORTSDIR}/sysutils/panicmail

DISTFILES=
NO_BUILD= yes
NO_INSTALL= yes
NOFETCHFILES= yes

PLIST_FILES= /usr/local/etc/panicmail.pub

WRKSRC= ${PWD}

do-install:
${INSTALL_SCRIPT} ${WRKSRC}/panicmail.pub ${STAGEDIR}${PREFIX}/etc/
${INSTALL_SCRIPT} ${WRKSRC}/rc.conf.panicmail ${STAGEDIR}${PREFIX}/etc/

.include <bsd.port.mk>
37 changes: 37 additions & 0 deletions Makefile.server
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
PORTNAME= panicmails-server
PORTVERSION= 0.2
CATEGORIES= sysutils

RUN_DEPENDS= nginx:${PORTSDIR}/www/nginx \
pkesh:${PORTSDIR}/security/pkesh

USE_PHP= yes

DISTFILES=
NO_BUILD= yes
NO_INSTALL= yes
NOFETCHFILES= yes

PLIST_FILES= /usr/local/etc/panicmail.key \
"@(,,0600) /usr/local/etc/panicfetchmail.rc" \
/usr/local/etc/panicprocmail.rc \
/usr/local/www/panics/panics.php \
/usr/local/www/panics/dec.sh \
/usr/local/etc/rc.conf.panicmail

PLIST_DIRS= /usr/local/www/panics/ \
/var/panicmail

WRKSRC= ${PWD}

do-install:
${INSTALL} -m600 ${WRKSRC}/panicfetchmail.rc ${STAGEDIR}${PREFIX}/etc/
${INSTALL_DATA} ${WRKSRC}/panicprocmail.rc ${STAGEDIR}${PREFIX}/etc/
${INSTALL_DATA} ${WRKSRC}/panicmail.key ${STAGEDIR}${PREFIX}/etc/
${INSTALL_DATA} ${WRKSRC}/rc.conf.panicmail ${STAGEDIR}${PREFIX}/etc/
${MKDIR} -p ${STAGEDIR}${PREFIX}/www/panics
${INSTALL_DATA} ${WRKSRC}/panics.php ${STAGEDIR}${PREFIX}/www/panics/
${INSTALL_SCRIPT} ${WRKSRC}/dec.sh ${STAGEDIR}${PREFIX}/www/panics/
${MKDIR} -p ${STAGEDIR}/var/panicmail

.include <bsd.port.mk>
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
# panicmails
panicmails
==========

server/client project to gather freebsd panics

Very early, but it seems to DTRT for now.

If an email appears in EMAILADDRESS, pull it down via fetchmail
and process it via procmail. If it matches on Kernel Panics, go ahead and decrypt.

Process unecrypted kernel panic and insert into local sqlite3 db.

Php will rendor all contents of the db at this time as I'm still testing.
-- Generating a key, pdkesh
* You need a private and public key for the email exchange
* Use pdkesh to generate them, named panicmail.pub and panicmail.key

-- building a package

I hacked together a port makefile that will embed some dependencies and allow
a local build from the git checkout, theoretically. This must be done on a machine
with a valid /usr/ports checkout (portsnap or svn co)
* sudo make clean
* cp Makefile.server Makefile
* sudo make install
* sudo make package
* work/pkg has your new package

* sudo make clean
* cp Makefile.client Makefile
* sudo make install
* sudo make package
* work/pkg has your new package

This will create a local package of this "stuff" that might even work
51 changes: 51 additions & 0 deletions dec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/sh

process_panicmail () {
# de-armor the mail first, strip away the headers and such
/usr/bin/perl -e 'while (<>) { last if (/ENCRYPTED FREEBSD PANIC DATA STARTS HERE/) };
while (<>) { exit 0 if (/ENCRYPTED FREEBSD PANIC DATA ENDS HERE/); print };' < $1 |
/usr/bin/perl -ne 'print "$1\n" if (/\|([^|]+)\|/)' |
/usr/bin/sed -e 's/=3D/=/g; s/[[:space:]]//' > $1.stripped

# decrypt and remove stripped file
/usr/local/bin/pkesh dec /usr/local/etc/panicmail.key $1.stripped $1.txt
rm $1.stripped

HTTP_ROOT=/usr/local/www/panics
PANICDB=$HTTP_ROOT/panics.db

if [ ! -f $PANICDB ]; then
/usr/local/bin/sqlite3 $PANICDB "create table panics (hostname, osrev, panicstr, date, epocdate, url);";
chown www:www $PANICDB;
fi

if [ -z $1 ]; then
exit 1
elif [ ! -f $1 ]; then
exit 1
fi


HOSTNAME=`grep Hostname $1.txt | awk -F": " '{print $2}'`
VERSION=`grep "Version String" $1.txt | awk -F": " '{print $2}' | awk '{print $2" "$4}'`
PANICSTR=`grep "Panic String" $1.txt | awk -F": " '{print $2}'`
PANICDATE=`grep Dumptime $1.txt | awk -F": " '{print $2}'`
EPOCDATE=`date -j -f "%a %b %d %T %Z %Y" "${PANICDATE}" "+%s"`
PANICDIR=/panictext/`date -j -f "%a %b %d %T %Z %Y" "${PANICDATE}" "+%Y-%m/%d"`

URL=${PANICDIR}/${HOSTNAME}.${EPOCDATE}.txt
if [ ! -d ${HTTP_ROOT}${PANICDIR} ]; then
mkdir -p ${HTTP_ROOT}${PANICDIR};
fi

mv $1.txt ${HTTP_ROOT}${URL}
/usr/local/bin/sqlite3 $PANICDB "insert into panics (hostname, osrev, panicstr, date, epocdate, url) \
values ('$HOSTNAME', '$VERSION', '$PANICSTR', '$PANICDATE', '$EPOCDATE', '$URL');"
}

if [ -f /var/panicmail/msg.* ]; then
for file in /var/panicmail/msg.*; do
process_panicmail $file;
rm $file;
done
fi
74 changes: 74 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<body>
<?php
echo "<H1>LLBSD Panic DB</H1>";
echo "<H2>Work in progress, poke sbruno@</H2>";

$db = new SQLite3("/usr/local/www/panics/panics.db");

// master table to next the other three
echo "<table cellpadding=0 border=0>";

echo '<td valign="top">';
// Count by host for date range of 24 hours
echo "<table cellpadding=10 border=1>";
echo "<tr>";
echo "<th align=center>Hostname</th>";
echo "<th>#</th>";
echo "</tr>";
$results = $db->query("SELECT hostname, count(hostname) FROM panics WHERE datetime(epocdate) >= date('now', '-1 day') group by hostname");
while ($paniccount = $results->fetchArray()) {
echo "<tr>";
echo "<td>".$paniccount['hostname']."</td>";
echo "<td>".$paniccount[1]."</td>";
echo "</tr>";
}
echo "</table>";
echo "</td>";
// end of first nested table row

echo '<td valign="top">';
// Count by osrev for date range of 24 hours
echo "<table cellpadding=10 border=1>";
echo "<tr>";
echo "<th align=center>OS Revision</th>";
echo "<th>#</th>";
echo "</tr>";
$results = $db->query("SELECT osrev, count(panicstr) FROM panics WHERE datetime(epocdate) >= date('now', '-1 day') group by osrev");
while ($paniccount = $results->fetchArray()) {
echo "<tr>";
echo "<td>".$paniccount['osrev']."</td>";
echo "<td>".$paniccount[1]."</td>";
echo "</tr>";
}
echo "</table>";
echo "</td>";

echo '<td valign="top">';
// main old table
echo "<table cellpadding=10 border=1>";

// print column headers
echo "<tr>";
echo "<th align=center>Hostname</th>";
echo "<th>Date</th>";
echo "<th>OS rev</th>";
echo "<th>Panic String</th>";
echo "</tr>";

$results = $db->query("SELECT * FROM panics WHERE datetime(epocdate) >= date('now', '-1 day') ORDER BY datetime(epocdate) DESC");
while ($panicrow = $results->fetchArray()) {
echo "<tr>";
//echo "<td>".$panicrow['hostname']."</td>";
echo "<td><a href='".$panicrow['url']."'>".$panicrow['hostname']."</a></td>";
echo "<td>".$panicrow['date']."</td>";
echo "<td>".$panicrow['osrev']."</td>";
echo "<td>".$panicrow['panicstr']."</td>";
echo "</tr>";
}


echo "</table>";
echo "</td>";
echo "</table>";
?>
</body>
9 changes: 9 additions & 0 deletions panicfetchmail.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
poll imap.gmail.com protocol IMAP
user "EMAILADDRESS" is root here
password 'EMAILPASSWORD'
folder 'INBOX'
fetchlimit 1
keep
ssl
sslcertfile /usr/local/share/certs/ca-root-nss.crt
mda '/usr/local/bin/procmail /usr/local/etc/panicprocmail.rc -f %F -d %T';
20 changes: 20 additions & 0 deletions panicprocmail.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# ensure /bin/sh for processing
SHELL=/bin/sh

# Set on when debugging
VERBOSE=yes

# The log of procmail actions
LOGFILE=/var/log/panicmail

INBOX=/var/panicmail

# Catch-all rule for all unmatched email
# Set EMAILADDRESS to the desintation inbox
:0:
* ^(From|Cc|To).*EMAILADDRESS
* ^Subject:.*Kernel Panic
$INBOX

:0
/dev/null
33 changes: 33 additions & 0 deletions panics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<body>
<?php
echo "<H1>LLBSD Panic DB</H1>";
echo "<H2>Work in progress, poke sbruno@</H2>";

$db = new SQLite3("/usr/local/www/panics/panics.db");

$results = $db->query('SELECT * from panics ORDER by datetime("epocdate") DESC');

echo "<table cellpadding=10 border=1>";

// print column headers
echo "<tr>";
echo "<td>Hostname</td>";
echo "<td>Date</td>";
echo "<td>OS rev</td>";
echo "<td>Panic String</td>";
echo "</tr>";

while ($panicrow = $results->fetchArray()) {
echo "<tr>";
//echo "<td>".$panicrow['hostname']."</td>";
echo "<td><a href='".$panicrow['url']."'>".$panicrow['hostname']."</a></td>";
echo "<td>".$panicrow['date']."</td>";
echo "<td>".$panicrow['osrev']."</td>";
echo "<td>".$panicrow['panicstr']."</td>";
echo "</tr>";
}


echo "</table>";
?>
</body>
2 changes: 2 additions & 0 deletions pkg-descr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
LLNW specific bits to parse panicmails sent from CDN and other FreeBSD nodes
into something that looks useable by a human being.
8 changes: 8 additions & 0 deletions rc.conf.panicmail
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
panicmail_enable="YES"
panicmail_autosubmit="YES"
panicmail_sendto="EMAILADDRESS"
panicmail_key="/usr/local/etc/panicmail.pub"

# requires nginx and php-fpm
nginx_enable="YES"
php_fpm_enable="YES"

0 comments on commit 83fa0ab

Please sign in to comment.