Howto: Using syslog for user created shell script - linux

Information abound about syslog, but I can't find anything very concise for my interest.
I have a user-created bash script that should log various debug, info, and error messages. I'd like to use syslog. This in Ubuntu Server distribution.
I'm looking for a quick overview only.
I see many files in /etc/logrotate.d that don't get discussed in any man pages that confuse me.
Should I be logging as user? local0-7?
Do I need to do something to configure this before I use these in a logger command?
How should I define what logs get created? Or is this already done?
With those questions answered I should be able to glean the details from the man pages.

You want the logger(1) utility, available in the bsdutils package.
From the man page:
logger - a shell command interface to the syslog(3) system log module
There's nothing that's essential to configure, just pass the switches you want. E.g.
logger -p local3.info -t myprogram "What's up, doc?"
You can now inspect wherever local3.info messages go and you will see something like this:
Jul 11 12:46:35 hostname myprogram: What's up, doc?
You only need to worry about logrotate if you need something fancier than this.
As for what log facility to use, I would use daemon for daemon messages and local for most other things. You should consult syslog(3) for the purposes of the different facilities.

Don't worry about logrotate. It doesn't affect you if you're logging to the system log.
You can use any facility you like. See the syslogd configuration for what ends up where.
See the syslogd configuration for what ends up where.
See the... yeah, you get it.

Related

Systemd unit configuration with shell expansion

I'm finally trying to convert my old init.d management script to systemd, and have run into a snag that I hope someone else has found a solution to.
My application expects an argument for the name of the file it saves logging information to, and my init script generates that name at startup with this line:
LOGFILE=`/bin/date "+$MUDDIR/lib/log/runlog.%y%m%d-%H%M%S"`
How would I go about having such a construct get run so the value is available for use in ExecStart?
Can I do something as simple as
ExecStartPre=LOGFILE=`/bin/date "+${MUDDIR}/lib/log/runlog.%y%m%d-%H%M%S"`
or would it throw a fit at trying to spawn a shell there? If it runs the command as a subshell, the value would be thrown away.
Also, if I specify multiple ExecStartPre lines, will they be executed in order?
Thanks for any hints. The documentation is a bit light on details in some places, and I'm just trying to preserve my original functionality without being cheesy and just having it call my old script.
I recommend letting systemd manage logging via its journal. Don't create a log file at all. Just have the application write to stdout/stderr. That way the logs will get stored alongside all the other applications' logs, they'll be searchable via journalctl, they'll show up in systemctl status <app>, they'll be rotated automatically, etc.
If the application absolutely requires a $LOGFILE environment variable you could use:
Environment=LOGFILE=/dev/stdout
I agree with OP's answer about letting journalctl handle logging. Rather than calling /dev/stdout in Environment attribute, use the following:
[Service]
...
...
StandardOutput=syslog
StandardError=syslog
This will put your logs into the system log, such as /var/log/messages and in journalctl. You can access the log using the service name:
journalctl -u myawesomeservice.service
As it seems, on newer (>238) systemd, you can set path to the log file.
StandardOutput=file:/path/to/logfile.log
StandardError=file:/path/to/logfile.err
Here is another SO question that provides much more info.

How to log to syslog?

Really basic Q. I come from largely working on Windows so, the vagueries of linux are still awaiting discovery by me. I have a centos 7 box, seems it has rsyslog. How do I send messages from a C++ program to syslog (or, rsyslog)? I have tried googling "linux log to syslog -logger -script" and it does not give me one single HOWTO reference. I get lots of "how to configure syslog to capture logs and send them to various files" type hits, but none to show how to use the logging mechanism itself. Looking for HOWTO's or illustrative code samples. Thanks!
See the syslog(3) man page for standard C Library functions and examples.

Processes started with systemd are logging to /var/log/syslog and /var/log/daemon.log

I'm running a couple of python scripts on a Beaglebone Black, both started at init by systemd. I noticed yesterday that my eMMC flash was completely full, and the reason was I had 1.8 gigs of log files. It seems that the standard output of these scripts (they generate a good amount of data for debugging purposes) is being logged to both /var/log/syslog and /var/log/daemon.log. Also, /var/log/messages has a ton of entries about "rate-limiting".
Now, I assume I could fix this by disabling standard output on these scripts. However, I would much rather preserve that capability and somehow tell systemd not to log that data. However, I've been unable to find information on this.
Here is an example of my current .service file:
[Unit]
Description=Description of my process
After=network.target
[Service]
Type=simple
ExecStart=/opt/myprocess
[Install]
WantedBy=multi-user.target
I should note that I originally had syslog.target as an "After" target, just because the example I was following had it. But I have since removed it and it doesn't seem to solve the problem.
Any help or insight into this problem would be greatly appreciated.
Edit: I may have found the answer here:
http://www.kibinlabs.com/systemd-logging-tricks/
Adding StandardOutput=null and seeing if that fixes it. Looks promising.
Edit 2: It does.
SystemD does not log files in /var/log by itself, ever.
If log messages are showing up in /var/log, this suggests that your system is running a "syslog" daemon, which is receiving log messages either (a) by reading the systemd journal, or (b) by applications using syslog directly.
Simply disabling your syslog daemon would prevent files from being written in /var/log. Messages will still be written to the system journal, but there are fairly flexible controls available to you to limit the space used by these files. See the journald docs for more information.

How do I determine whether linux process accounting (accton) is currently running?

Is there any way to determine whether process accounting (accton) is running? There is no process listed in the process table ("ps"), and I see nothing under "/etc" that I can call with "status" to get a status of accounting.
I'm running a custom build based on "Linux From Scratch", so while I understand that CentOS has "psacct", I don't have that available.
I could watch the log file and see if it's growing - not ideal, but if it's all I got, then it's all I got. I'm hoping there's a better way.
Appreciate any info.
May be you can do like this. enable logging for your accton, an check the log file for update. like time stamp modification/lat access time/modify time ?
/sbin/accton /var/log/accto.log then the log will be created.
Then use some script to monitor your accto.log. There are lot of log file monitoring scripts for nagios if you are interested.

Call Visitors web stat program from PHP

I've been looking into different web statistics programs for my site, and one promising one is Visitors. Unfortunately, it's a C program and I don't know how to call it from the web server. I've tried using PHP's shell_exec, but my web host (NFSN) has PHP's safe mode on and it's giving me an error message.
Is there a way to execute the program within safe mode? If not, can it work with CGI? If so, how? (I've never used CGI before)
Visitors looks like a log analyzer and report generator. Its probably best setup as a chron job to create static HTML pages once a day or so.
If you don't have shell access to your hosting account, or some sort of control panel that lets you setup up chron jobs, you'll be out of luck.
Is there any reason not to just use Google Analytics? It's free, and you don't have to write it yourself. I use it, and it gives you a lot of information.
Sorry, I know it's not a "programming" answer ;)
I second the answer of Jonathan: this is a log analyzer, meaning that you must feed it as input the logfile of the webserver and it generates a summarization of it. Given that you are on a shared host, it is improbable that you can access to that file, and even if you would access it, it is probable that it contains then entries for all the websites hosted on the given machine (setting up separate logging for each VirtualHost is certainly possible with Apache, but I don't know if it is a common practice).
One possible workaround would be for you to write out a logfile from your pages. However this is rather difficult and can have a severe performance impact (you have to serialize the writes to the logfile for one, if you don't want to get garbage from time to time). All in all, I would suggest going with an online analytics service, like Google Analytics.
As fortune would have it I do have access to the log file for my site. I've been able to generate the HTML page on the server manually - I've just been looking for a way to get it to happen automatically. All I need is to execute a shell command and get the output to display as the page.
Sounds like a good job for an intern.
=)
Call your host and see if you can work out a deal for doing a shell execute.
I managed to solve this problem on my own. I put the following lines in a file named visitors.cgi:
#!/bin/sh
printf "Content-type: text/html\n\n"
exec visitors -A /home/logs/access_log

Resources