# -*- text -*-
##
## counter.conf -- PostgreSQL queries for rlm_sqlcounter
##
##	$Id$

#  Rather than maintaining seperate (GDBM) databases of
#  accounting info for each counter, this module uses the data
#  stored in the raddacct table by the sql modules. This
#  module NEVER does any database INSERTs or UPDATEs.  It is
#  totally dependent on the SQL module to process Accounting
#  packets.
#
#  The 'sqlmod_inst' parameter holds the instance of the sql
#  module to use when querying the SQL database. Normally it
#  is just "sql".  If you define more and one SQL module
#  instance (usually for failover situations), you can
#  specify which module has access to the Accounting Data
#  (radacct table).
#
#  The 'reset' parameter defines when the counters are all
#  reset to zero.  It can be hourly, daily, weekly, monthly or
#  never.  It can also be user defined. It should be of the
#  form:
#  	num[hdwm] where:
#  	h: hours, d: days, w: weeks, m: months
#  	If the letter is ommited days will be assumed. In example:
#  	reset = 10h (reset every 10 hours)
#  	reset = 12  (reset every 12 days)
#
#  The 'key' parameter specifies the unique identifier for the
#  counter records (usually 'User-Name').
#
#  The 'query' parameter specifies the SQL query used to get
#  the current Counter value from the database. There are 3
#  parameters that can be used in the query:
#		%k	'key' parameter
#		%b	unix time value of beginning of reset period
#		%e	unix time value of end of reset period
#
#  The 'check-name' parameter is the name of the 'check'
#  attribute to use to access the counter in the 'users' file
#  or SQL radcheck or radcheckgroup tables.
#
#  DEFAULT  Max-Daily-Session > 3600, Auth-Type = Reject
#      Reply-Message = "You've used up more than one hour today"
#
sqlcounter dailycounter {
	counter-name = Daily-Session-Time
	check-name = Max-Daily-Session
	reply-name = Session-Timeout
	sqlmod-inst = sql
	key = User-Name
	reset = daily

	# This query properly handles calls that span from the
	# previous reset period into the current period but
	# involves more work for the SQL server than those
	# below
	query = "SELECT IFNULL(SUM(acctsessiontime - \
                 GREATEST((%b - UNIX_TIMESTAMP(acctstarttime)), 0)),0) \
                 FROM radacct WHERE username = '%{%k}' AND \
                 UNIX_TIMESTAMP(acctstarttime) + acctsessiontime > '%b'"

	# This query ignores calls that started in a previous
	# reset period and continue into into this one. But it
	# is a little easier on the SQL server
#	query = "SELECT SUM(acctsessiontime) FROM radacct WHERE \
#                username = '%{%k}' AND acctstarttime > FROM_UNIXTIME('%b')"

	# This query is the same as above, but demonstrates an
	# additional counter parameter '%e' which is the
	# timestamp for the end of the period
#	query = "SELECT SUM(acctsessiontime) FROM radacct \
#                WHERE username = '%{%k}' AND acctstarttime BETWEEN \
#                FROM_UNIXTIME('%b') AND FROM_UNIXTIME('%e')"
}

sqlcounter monthlycounter {
	counter-name = Monthly-Session-Time
		check-name = Max-Monthly-Session
		reply-name = Session-Timeout
		sqlmod-inst = sql
		key = User-Name
		reset = monthly

	# This query properly handles calls that span from the
	# previous reset period into the current period but
	# involves more work for the SQL server than those
	# below
	query = "SELECT IFNULL(SUM(acctsessiontime - \
                 GREATEST((%b - UNIX_TIMESTAMP(acctstarttime)), 0)),0) \
                 FROM radacct WHERE username='%{%k}' AND \
                 UNIX_TIMESTAMP(acctstarttime) + acctsessiontime > '%b'"

	# This query ignores calls that started in a previous
	# reset period and continue into into this one. But it
	# is a little easier on the SQL server
#	query = "SELECT SUM(acctsessiontime) FROM radacct WHERE \
#                username='%{%k}' AND acctstarttime > FROM_UNIXTIME('%b')"

	# This query is the same as above, but demonstrates an
	# additional counter parameter '%e' which is the
	# timestamp for the end of the period
#	query = "SELECT SUM(acctsessiontime) FROM radacct \
#                WHERE username='%{%k}' AND acctstarttime BETWEEN \
#                FROM_UNIXTIME('%b') AND FROM_UNIXTIME('%e')"
}

	sqlcounter weeklycounter {
               counter-name = Weekly-Session-Time
               check-name = Max-Weekly-Session
               sqlmod-inst = sql
               key = User-Name
               reset = weekly
               query = "SELECT IFNULL(SUM(AcctSessionTime),0) FROM radacct WHERE UserName='%{%k}'"
       }
	   
	   
	sqlcounter quaterlycounter {
               counter-name = Quaterly-Session-Time
               check-name = Max-Quaterly-Session
               sqlmod-inst = sql
               key = User-Name
               reset = 3m
               query = "SELECT IFNULL(SUM(AcctSessionTime),0) FROM radacct WHERE UserName='%{%k}'"
       }
	   

	sqlcounter yearlycounter {
               counter-name = Yearly-Session-Time
               check-name = Max-Yearly-Session
               sqlmod-inst = sql
               key = User-Name
               reset = 12m
               query = "SELECT IFNULL(SUM(AcctSessionTime),0) FROM radacct WHERE UserName='%{%k}'"
       }


	sqlcounter noresetcounter {
               counter-name = Max-All-Session-Time
               check-name = Max-All-Session
               sqlmod-inst = sql
               key = User-Name
               reset = never
               query = "SELECT IFNULL(SUM(AcctSessionTime),0) FROM radacct WHERE UserName='%{%k}'"
       }

	sqlcounter accessperiod {

                counter-name = Max-Access-Period-Never
                check-name = Access-Period
                sqlmod-inst = sql
                key = User-Name
                reset = never
                query = "SELECT IF(COUNT(radacctid>=1),(UNIX_TIMESTAMP() - IFNULL(UNIX_TIMESTAMP(AcctStartTime),0)),0) FROM radacct WHERE UserName = '%{%k}' AND AcctSessionTime >= 1 ORDER BY AcctStartTime LIMIT 1"                
        }


	sqlcounter counterChilliSpotMaxTotalOctetsDaily {
			counter-name = ChilliSpot-Max-Total-Octets-Daily
			check-name = CS-Total-Octets-Daily
			counter-type = data
			reply-name = ChilliSpot-Max-Total-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = daily
			query = "SELECT IFNULL((SUM(AcctInputOctets + AcctOutputOctets)),0) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}


	sqlcounter counterChilliSpotMaxTotalOctetsWeekly {
			counter-name = ChilliSpot-Max-Total-Octets-Weekly
			check-name = CS-Total-Octets-Weekly
			counter-type = data
			reply-name = ChilliSpot-Max-Total-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = weekly
			query = "SELECT IFNULL((SUM(AcctInputOctets + AcctOutputOctets)),0) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}


	sqlcounter counterChilliSpotMaxTotalOctetsMonthly {
			counter-name = ChilliSpot-Max-Total-Octets-Monthly
			check-name = CS-Total-Octets-Monthly
			counter-type = data
			reply-name = ChilliSpot-Max-Total-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = monthly
			query = "SELECT IFNULL((SUM(AcctInputOctets + AcctOutputOctets)),0)FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}

	
	sqlcounter counterChilliSpotMaxTotalOctetsQuarterly {
			counter-name = ChilliSpot-Max-Total-Octets-Quarterly
			check-name = CS-Total-Octets-Quarterly
			counter-type = data
			reply-name = ChilliSpot-Max-Total-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = 3m
			query = "SELECT IFNULL((SUM(AcctInputOctets + AcctOutputOctets)),0) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}

	
	sqlcounter counterChilliSpotMaxTotalOctetsYearly {
			counter-name = ChilliSpot-Max-Total-Octets-Yearly
			check-name = CS-Total-Octets-Yearly
			counter-type = data
			reply-name = ChilliSpot-Max-Total-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = 12m
			query = "SELECT IFNULL((SUM(AcctInputOctets + AcctOutputOctets)),0) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}
	
	sqlcounter counterChilliSpotMaxTotalOctetsAll {
			counter-name = ChilliSpot-Max-Total-Octets
			check-name = CS-Total-Octets
			counter-type = data
			reply-name = ChilliSpot-Max-Total-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = never
			query = "SELECT IFNULL((SUM(AcctInputOctets + AcctOutputOctets)),0) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}


	sqlcounter counterChilliSpotMaxInputOctetsDaily {
			counter-name = ChilliSpot-Max-Input-Octets-Daily
			check-name = CS-Input-Octets-Daily
			counter-type = data
			reply-name = ChilliSpot-Max-Input-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = daily
			query = "SELECT IFNULL(SUM(AcctInputOctets),0) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}


	sqlcounter counterChilliSpotMaxInputOctetsWeekly {
			counter-name = ChilliSpot-Max-Input-Octets-Weekly
			check-name = CS-Input-Octets-Weekly
			counter-type = data
			reply-name = ChilliSpot-Max-Input-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = weekly
			query = "SELECT IFNULL(SUM(AcctInputOctets),0) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}


	sqlcounter counterChilliSpotMaxInputOctetsMonthly {
			counter-name = ChilliSpot-Max-Input-Octets-Monthly
			check-name = CS-Input-Octets-Monthly
			counter-type = data
			reply-name = ChilliSpot-Max-Input-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = monthly
			query = "SELECT IFNULL(SUM(AcctInputOctets),0) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}

	
	sqlcounter counterChilliSpotMaxInputOctetsQuarterly {
			counter-name = ChilliSpot-Max-Input-Octets-Quarterly
			check-name = CS-Input-Octets-Quarterly
			counter-type = data
			reply-name = ChilliSpot-Max-Input-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = 3m
			query = "SELECT IFNULL(SUM(AcctInputOctets),0) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}

	
	sqlcounter counterChilliSpotMaxInputOctetsYearly {
			counter-name = ChilliSpot-Max-Input-Octets-Yearly
			check-name = CS-Input-Octets-Yearly
			counter-type = data
			reply-name = ChilliSpot-Max-Input-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = 12m
			query = "SELECT IFNULL(SUM(AcctInputOctets),0) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}
	
	sqlcounter counterChilliSpotMaxInputOctetsAll {
			counter-name = ChilliSpot-Max-Input-Octets
			check-name = CS-Input-Octets
			counter-type = data
			reply-name = ChilliSpot-Max-Input-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = never
			query = "SELECT IFNULL(SUM(AcctInputOctets),0) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}


	sqlcounter counterChilliSpotMaxOutputOctetsDaily {
			counter-name = ChilliSpot-Max-Output-Octets-Daily
			check-name = CS-Output-Octets-Daily
			counter-type = data
			reply-name = ChilliSpot-Max-Output-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = daily
			query = "SELECT IFNULL(SUM(AcctOutputOctets),0) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}


	sqlcounter counterChilliSpotMaxOutputOctetsWeekly {
			counter-name = ChilliSpot-Max-Output-Octets-Weekly
			check-name = CS-Output-Octets-Weekly
			counter-type = data
			reply-name = ChilliSpot-Max-Output-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = weekly
			query = "SELECT IFNULL(SUM(AcctOutputOctets),0) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}


	sqlcounter counterChilliSpotMaxOutputOctetsMonthly {
			counter-name = ChilliSpot-Max-Output-Octets-Monthly
			check-name = CS-Output-Octets-Monthly
			counter-type = data
			reply-name = ChilliSpot-Max-Output-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = monthly
			query = "SELECT IFNULL(SUM(AcctOutputOctets),0) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}

	
	sqlcounter counterChilliSpotMaxOutputOctetsQuarterly {
			counter-name = ChilliSpot-Max-Output-Octets-Quarterly
			check-name = CS-Output-Octets-Quarterly
			counter-type = data
			reply-name = ChilliSpot-Max-Output-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = 3m
			query = "SELECT IFNULL(SUM(AcctOutputOctets),0) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}

	
	sqlcounter counterChilliSpotMaxOutputOctetsYearly {
			counter-name = ChilliSpot-Max-Output-Octets-Yearly
			check-name = CS-Output-Octets-Yearly
			counter-type = data
			reply-name = ChilliSpot-Max-Output-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = 12m
			query = "SELECT IFNULL(SUM(AcctOutputOctets),0) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}
	
	sqlcounter counterChilliSpotMaxOutputOctetsAll {
			counter-name = ChilliSpot-Max-Output-Octets
			check-name = CS-Output-Octets
			counter-type = data
			reply-name = ChilliSpot-Max-Output-Octets
			sqlmod-inst = sql
			key = User-Name
			reset = never
			query = "SELECT IFNULL(SUM(AcctOutputOctets),0) FROM radacct WHERE UserName='%{%k}' AND UNIX_TIMESTAMP(AcctStartTime) + AcctSessionTime > '%b'"
	}
