b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #!/bin/sh /etc/rc.common |
| 2 | # Copyright (C) 2008-2017 OpenWrt.org |
| 3 | |
| 4 | START=99 |
| 5 | |
| 6 | USE_PROCD=1 |
| 7 | |
| 8 | CONFIGNAME="bandwidthd" |
| 9 | CONFIGPHPNAME="bandwidthd-php" |
| 10 | CONFIGFILE="/tmp/etc/bandwidthd.conf" |
| 11 | CONFIGPHPFILE="/tmp/etc/bandwidthd-php.conf" |
| 12 | |
| 13 | config_cb() { |
| 14 | local cfg_type="$1" |
| 15 | local cfg_name="$2" |
| 16 | |
| 17 | case "$cfg_type" in |
| 18 | bandwidthd) |
| 19 | append cfgs "$cfg_name" |
| 20 | ;; |
| 21 | esac |
| 22 | } |
| 23 | |
| 24 | export_bool() { |
| 25 | local option="$1" |
| 26 | local section="$2" |
| 27 | local _loctmp |
| 28 | config_get_bool _loctmp "$section" "$option" |
| 29 | if [ -n "$_loctmp" ]; then |
| 30 | conffile="${conffile}$option " |
| 31 | if [ 1 -eq "$_loctmp" ]; then |
| 32 | conffile="${conffile}true" |
| 33 | else |
| 34 | conffile="${conffile}false" |
| 35 | fi |
| 36 | conffile="${conffile}\n" |
| 37 | fi |
| 38 | } |
| 39 | |
| 40 | export_number() { |
| 41 | local option="$1" |
| 42 | local section="$2" |
| 43 | local _loctmp |
| 44 | config_get _loctmp "$section" "$option" |
| 45 | if [ -n "$_loctmp" ]; then |
| 46 | conffile="${conffile}$option ${_loctmp}\n" |
| 47 | fi |
| 48 | } |
| 49 | |
| 50 | export_string() { |
| 51 | local option="$1" |
| 52 | local section="$2" |
| 53 | local _loctmp |
| 54 | config_get _loctmp "$section" "$option" |
| 55 | if [ -n "$_loctmp" ]; then |
| 56 | conffile="${conffile}$option \"${_loctmp}\"\n" |
| 57 | fi |
| 58 | } |
| 59 | |
| 60 | set_conf_file() { |
| 61 | local subnet conffile="# auto-generated config file from /etc/config/${CONFIGNAME}\n" |
| 62 | |
| 63 | [ -d /tmp/bandwidthd ] || mkdir -p /tmp/bandwidthd && cp /www/legend.gif /www/logo.gif /tmp/bandwidthd/ |
| 64 | [ -e /htdocs ] || ln -s /tmp/bandwidthd /htdocs |
| 65 | [ -e /www/bandwidthd ] || ln -s /tmp/bandwidthd /www/bandwidthd |
| 66 | rm -f $CONFIGFILE |
| 67 | touch $CONFIGFILE |
| 68 | [ -e /etc/bandwidthd.conf ] || ln -s $CONFIGFILE /etc/bandwidthd.conf |
| 69 | |
| 70 | config_load $CONFIGNAME |
| 71 | for cfg in $cfgs; do |
| 72 | config_get subnets $cfg subnets |
| 73 | for subnet in $subnets; do |
| 74 | conffile="${conffile}subnet ${subnet}\n" |
| 75 | done |
| 76 | export_string dev $cfg |
| 77 | export_number skip_intervals $cfg |
| 78 | export_number graph_cutoff $cfg |
| 79 | export_bool promiscuous $cfg |
| 80 | export_bool output_cdf $cfg |
| 81 | export_bool recover_cdf $cfg |
| 82 | export_string filter $cfg |
| 83 | export_bool graph $cfg |
| 84 | export_number meta_refresh $cfg |
| 85 | export_string pgsql_connect_string $cfg |
| 86 | export_string sqlite_filename $cfg |
| 87 | export_string sensor_id $cfg |
| 88 | [ -n "$conffile" ] && echo -e "$conffile" >>$CONFIGFILE |
| 89 | unset conffile |
| 90 | done |
| 91 | cd / |
| 92 | } |
| 93 | |
| 94 | set_php_conf_file() { |
| 95 | local subnet conffile="<?php\n// auto-generated config file from /etc/config/${CONFIGPHPNAME}\n" |
| 96 | |
| 97 | rm -f $CONFIGPHPFILE |
| 98 | touch $CONFIGPHPFILE |
| 99 | |
| 100 | config_load $CONFIGPHPNAME |
| 101 | for cfg in $cfgs; do |
| 102 | export_number dflt_width $cfg |
| 103 | conffile="${conffile}define(\"DFLT_WIDTH\", ${paramstr:-"900"});\n" |
| 104 | |
| 105 | export_number dflt_height $cfg |
| 106 | conffile="${conffile}define(\"DFLT_HEIGHT\", ${paramstr:-"256"});\n" |
| 107 | |
| 108 | export_string dflt_interval $cfg |
| 109 | conffile="${conffile}define(\"DFLT_INTERVAL\", ${paramstr:-"INT_DAILY"});\n\n"'$db_connect_string = "sqlite:' |
| 110 | |
| 111 | export_string sqlite_dbname $cfg |
| 112 | conffile="${conffile}${paramstr:-"/www/bandwidthd/stats.db"}"'"'"\n?>" |
| 113 | [ -n "$conffile" ] && echo -e "$conffile" >>$CONFIGPHPFILE |
| 114 | done |
| 115 | cd / |
| 116 | } |
| 117 | |
| 118 | service_triggers() { |
| 119 | procd_add_reload_trigger $CONFIGNAME |
| 120 | procd_add_reload_trigger $CONFIGPHPNAME |
| 121 | } |
| 122 | |
| 123 | start_service() { |
| 124 | set_conf_file |
| 125 | set_php_conf_file |
| 126 | procd_open_instance |
| 127 | procd_set_param command /usr/sbin/bandwidthd |
| 128 | procd_close_instance |
| 129 | } |
| 130 | |
| 131 | stop_service() { |
| 132 | service_stop /usr/sbin/bandwidthd |
| 133 | } |