install-eye.sh 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. #!/bin/bash
  2. # Eye Installation Script for ALT Linux/Debian/Ubuntu with PostgreSQL support
  3. # Version: 2.1
  4. set -e
  5. # Colors for output
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. BLUE='\033[0;34m'
  10. NC='\033[0m' # No Color
  11. # Global variables
  12. MYSQL_PASSWORD=""
  13. POSTGRES_PASSWORD=""
  14. NA
  15. DB_TYPE="mysql" # Default database type
  16. DB_NAME="stat"
  17. DB_USER="stat"
  18. # Output functions
  19. print_info() {
  20. echo -e "${GREEN}[INFO]${NC} $1"
  21. }
  22. print_warn() {
  23. echo -e "${YELLOW}[WARN]${NC} $1"
  24. }
  25. print_error() {
  26. echo -e "${RED}[ERROR]${NC} $1"
  27. }
  28. print_step() {
  29. echo -e "${BLUE}=== $1 ===${NC}"
  30. }
  31. # Check for root privileges
  32. check_root() {
  33. if [[ $EUID -ne 0 ]]; then
  34. print_error "This script must be run as root"
  35. print_error "Use: sudo $0"
  36. exit 1
  37. fi
  38. }
  39. # Detect distribution and package manager
  40. detect_distro() {
  41. if [[ -f /etc/os-release ]]; then
  42. . /etc/os-release
  43. OS_ID=$ID
  44. OS_VERSION=$VERSION_ID
  45. OS_NAME=$NAME
  46. case $OS_ID in
  47. altlinux)
  48. PACKAGE_MANAGER="apt-get"
  49. SERVICE_MANAGER="systemctl"
  50. OS_FAMILY="alt"
  51. print_info "Detected ALT Linux $OS_VERSION"
  52. ;;
  53. debian)
  54. PACKAGE_MANAGER="apt"
  55. SERVICE_MANAGER="systemctl"
  56. OS_FAMILY="debian"
  57. print_info "Detected Debian $OS_VERSION"
  58. ;;
  59. ubuntu)
  60. PACKAGE_MANAGER="apt"
  61. SERVICE_MANAGER="systemctl"
  62. OS_FAMILY="debian"
  63. print_info "Detected Ubuntu $OS_VERSION"
  64. ;;
  65. *)
  66. print_error "Unsupported distribution: $OS_ID"
  67. print_error "Supported: ALT Linux, Debian, Ubuntu"
  68. exit 1
  69. ;;
  70. esac
  71. else
  72. print_error "Failed to detect distribution"
  73. exit 1
  74. fi
  75. }
  76. select_language_with_auto() {
  77. print_step "Select Installation Language"
  78. # Проверка автоматического режима
  79. if [[ "$AUTO_MODE" == "true" ]]; then
  80. EYE_LANG="english"
  81. EYE_LANG_SHORT="en"
  82. print_info "Auto mode: English language selected by default"
  83. return 0
  84. fi
  85. echo "Available languages:"
  86. echo "1) English"
  87. echo "2) Russian (default)"
  88. echo ""
  89. while true; do
  90. read -p "Select language (1 or 2) [2]: " lang_choice
  91. # Если пустой ввод - по умолчанию английский
  92. if [[ -z "$lang_choice" ]]; then
  93. lang_choice="2"
  94. fi
  95. # Обработка ввода (приводим к нижнему регистру)
  96. lang_choice_lower=$(echo "$lang_choice" | tr '[:upper:]' '[:lower:]')
  97. case $lang_choice_lower in
  98. 1|english|en|eng|анг|английский)
  99. EYE_LANG="english"
  100. EYE_LANG_SHORT="en"
  101. print_info "Selected English language"
  102. break
  103. ;;
  104. 2|russian|ru|rus|ру|русский)
  105. EYE_LANG="russian"
  106. EYE_LANG_SHORT="ru"
  107. print_info "Selected Russian language (Русский)"
  108. break
  109. ;;
  110. *)
  111. print_error "Invalid choice: '$lang_choice'"
  112. print_warn "Available options: 1 (English), 2 (Russian)"
  113. print_warn "You can also type: english, en, russian, ru"
  114. ;;
  115. esac
  116. done
  117. }
  118. # Ask user for database type
  119. select_database_type() {
  120. print_step "Select Database Type"
  121. echo "Available database types:"
  122. echo "1) MySQL/MariaDB (default)"
  123. echo "2) PostgreSQL"
  124. echo ""
  125. read -p "Select database type (1 or 2) [1]: " db_choice
  126. case $db_choice in
  127. 2|postgres|postgresql|pgsql)
  128. DB_TYPE="postgresql"
  129. print_info "Selected PostgreSQL"
  130. ;;
  131. *)
  132. DB_TYPE="mysql"
  133. print_info "Selected MySQL/MariaDB"
  134. ;;
  135. esac
  136. }
  137. # Install dependencies for ALT Linux
  138. install_deps_altlinux() {
  139. print_step "Installing dependencies for ALT Linux"
  140. # Update repositories
  141. apt-get update
  142. # General utilities
  143. apt-get install -y git xxd wget fping hwdata
  144. # Database installation based on selected type
  145. if [[ "$DB_TYPE" == "postgresql" ]]; then
  146. apt-get install -y postgresql postgresql-client
  147. else
  148. apt-get install -y mariadb-server mariadb-client
  149. fi
  150. # Web server and PHP
  151. if [[ "$DB_TYPE" == "postgresql" ]]; then
  152. apt-get install -y apache2 \
  153. php8.2 php8.2-pgsql php8.2-pdo-pgsql php8.2-intl php8.2-mbstring \
  154. pear-Mail php8.2-snmp php8.2-zip \
  155. php8.2-fpm-fcgi apache2-mod_fcgid
  156. else
  157. apt-get install -y apache2 \
  158. php8.2 php8.2-mysqlnd php8.2-intl php8.2-mbstring \
  159. pear-Mail php8.2-snmp php8.2-zip \
  160. php8.2-pgsql php8.2-mysqlnd php8.2-pdo_mysql php8.2-mysqlnd-mysqli \
  161. php8.2-fpm-fcgi apache2-mod_fcgid
  162. fi
  163. # Perl modules
  164. if [[ "$DB_TYPE" == "postgresql" ]]; then
  165. apt-get install -y perl perl-Net-Patricia perl-NetAddr-IP \
  166. perl-Config-Tiny perl-Net-DNS perl-DateTime perl-Net-Ping \
  167. perl-Net-Netmask perl-Text-Iconv perl-Net-SNMP \
  168. perl-Net-Telnet perl-DBI perl-DBD-Pg \
  169. perl-Parallel-ForkManager perl-Proc-Daemon \
  170. perl-DateTime-Format-DateParse \
  171. perl-Net-OpenSSH perl-File-Tail perl-Crypt-Rijndael \
  172. perl-Crypt-CBC perl-CryptX perl-Crypt-DES \
  173. perl-File-Path-Tiny perl-Expect \
  174. perl-Proc-ProcessTable
  175. else
  176. apt-get install -y perl perl-Net-Patricia perl-NetAddr-IP \
  177. perl-Config-Tiny perl-Net-DNS perl-DateTime perl-Net-Ping \
  178. perl-Net-Netmask perl-Text-Iconv perl-Net-SNMP \
  179. perl-Net-Telnet perl-DBI perl-DBD-mysql perl-DBD-Pg \
  180. perl-Parallel-ForkManager perl-Proc-Daemon \
  181. perl-DateTime-Format-DateParse \
  182. perl-Net-OpenSSH perl-File-Tail perl-Crypt-Rijndael \
  183. perl-Crypt-CBC perl-CryptX perl-Crypt-DES \
  184. perl-File-Path-Tiny perl-Expect \
  185. perl-Proc-ProcessTable
  186. fi
  187. # Additional services
  188. apt-get install -y dnsmasq syslog-ng syslog-ng-journal
  189. # Install pwgen if not present
  190. if ! command -v pwgen &> /dev/null; then
  191. apt-get install -y pwgen
  192. fi
  193. control fping public
  194. control ping public
  195. }
  196. # Install dependencies for Debian/Ubuntu
  197. install_deps_debian() {
  198. print_step "Installing dependencies for Debian/Ubuntu"
  199. # Update repositories
  200. apt-get update
  201. # General utilities
  202. apt-get install -y git xxd bsdmainutils pwgen wget fping ieee-data
  203. # Database installation based on selected type
  204. if [[ "$DB_TYPE" == "postgresql" ]]; then
  205. apt-get install -y postgresql postgresql-client
  206. else
  207. apt-get install -y mariadb-server mariadb-client
  208. fi
  209. # Web server and PHP
  210. if [[ "$DB_TYPE" == "postgresql" ]]; then
  211. apt-get install -y apache2 \
  212. php php-pgsql php-bcmath php-intl php-mbstring \
  213. php-date php-mail php-snmp php-zip \
  214. php-db php-fpm libapache2-mod-fcgid
  215. else
  216. apt-get install -y apache2 \
  217. php php-mysql php-bcmath php-intl php-mbstring \
  218. php-date php-mail php-snmp php-zip \
  219. php-db php-pgsql php-fpm libapache2-mod-fcgid
  220. fi
  221. # Perl modules
  222. if [[ "$DB_TYPE" == "postgresql" ]]; then
  223. apt-get install -y perl libnet-patricia-perl libnetaddr-ip-perl \
  224. libconfig-tiny-perl libnet-dns-perl libdatetime-perl \
  225. libnet-netmask-perl libtext-iconv-perl libnet-snmp-perl \
  226. libnet-telnet-perl libdbi-perl \
  227. libparallel-forkmanager-perl libproc-daemon-perl \
  228. libdatetime-format-dateparse-perl \
  229. libnet-openssh-perl libfile-tail-perl libcrypt-rijndael-perl \
  230. libcrypt-cbc-perl libcryptx-perl libdbd-pg-perl \
  231. libfile-path-tiny-perl libexpect-perl libcrypt-des-perl
  232. else
  233. apt-get install -y perl libnet-patricia-perl libnetaddr-ip-perl \
  234. libconfig-tiny-perl libnet-dns-perl libdatetime-perl \
  235. libnet-netmask-perl libtext-iconv-perl libnet-snmp-perl \
  236. libnet-telnet-perl libdbi-perl libdbd-mysql-perl \
  237. libparallel-forkmanager-perl libproc-daemon-perl \
  238. libdatetime-format-dateparse-perl \
  239. libnet-openssh-perl libfile-tail-perl libcrypt-rijndael-perl \
  240. libcrypt-cbc-perl libcryptx-perl libdbd-pg-perl \
  241. libfile-path-tiny-perl libexpect-perl libcrypt-des-perl
  242. fi
  243. # Additional services
  244. apt-get install -y dnsmasq syslog-ng
  245. }
  246. # System update
  247. update_system() {
  248. print_step "Updating system"
  249. $PACKAGE_MANAGER update -y
  250. }
  251. # Install packages
  252. install_packages() {
  253. print_step "Installing packages"
  254. case $OS_FAMILY in
  255. alt)
  256. install_deps_altlinux
  257. ;;
  258. debian)
  259. install_deps_debian
  260. ;;
  261. esac
  262. }
  263. # Create user and group
  264. create_user_group() {
  265. print_step "Creating user and group"
  266. # Create group
  267. if ! getent group eye >/dev/null; then
  268. groupadd --system eye
  269. print_info "Group 'eye' created"
  270. else
  271. print_info "Group 'eye' already exists"
  272. fi
  273. # Create user
  274. if ! id -u eye >/dev/null 2>&1; then
  275. if [[ "$OS_FAMILY" == "alt" ]]; then
  276. # For ALT Linux
  277. useradd --system --shell /bin/bash --home-dir /opt/Eye \
  278. --gid eye --groups eye eye
  279. else
  280. # For Debian/Ubuntu
  281. adduser --system --disabled-password --disabled-login \
  282. --ingroup eye --home=/opt/Eye eye
  283. fi
  284. print_info "User 'eye' created"
  285. else
  286. print_info "User 'eye' already exists"
  287. fi
  288. # Create directory
  289. mkdir -p /opt/Eye
  290. chown eye:eye /opt/Eye
  291. chmod 770 /opt/Eye
  292. # Add nagios to eye group (if exists)
  293. if id -u nagios >/dev/null 2>&1; then
  294. usermod -a -G eye nagios
  295. print_info "User 'nagios' added to group 'eye'"
  296. fi
  297. }
  298. # Check and apply SNMP SHA512 patch
  299. apply_snmp_patch() {
  300. print_info "Checking for SNMPv3 SHA512 support..."
  301. # File paths
  302. USM_PATCH_FILE="/opt/Eye/docs/patches/sha512.patch"
  303. if [[ "$OS_FAMILY" == "alt" ]]; then
  304. USM_PATCH_FILE="/opt/Eye/docs/patches/sha512.alt.patch"
  305. fi
  306. USM_PM_FILE=""
  307. # Search for USM.pm in system
  308. local usm_paths=(
  309. "/usr/share/perl5/Net/SNMP/Security/USM.pm"
  310. "/usr/lib/perl5/vendor_perl/Net/SNMP/Security/USM.pm"
  311. "/usr/local/share/perl5/Net/SNMP/Security/USM.pm"
  312. )
  313. for path in "${usm_paths[@]}"; do
  314. if [[ -f "$path" ]]; then
  315. USM_PM_FILE="$path"
  316. print_info "Found USM.pm: $USM_PM_FILE"
  317. break
  318. fi
  319. done
  320. if [[ -z "$USM_PM_FILE" ]]; then
  321. print_warn "USM.pm file not found in system"
  322. return 1
  323. fi
  324. # Check if patch already applied
  325. if grep -q "AUTH_PROTOCOL_HMACSHA512" "$USM_PM_FILE"; then
  326. print_info "SHA512 patch already applied"
  327. return 0
  328. fi
  329. # Create backup
  330. cp "$USM_PM_FILE" "${USM_PM_FILE}.backup"
  331. print_info "Backup created: ${USM_PM_FILE}.backup"
  332. # Try to apply patch file
  333. local patch_applied=false
  334. if [[ -f "$USM_PATCH_FILE" ]]; then
  335. print_info "Attempting to apply patch from $USM_PATCH_FILE"
  336. # Check if patch can be applied
  337. if patch --dry-run -l -p1 -i "$USM_PATCH_FILE" -r /tmp/patch.rej "$USM_PM_FILE" 2>/dev/null; then
  338. # Apply patch
  339. if patch -l -p1 -i "$USM_PATCH_FILE" "$USM_PM_FILE" 2>/dev/null; then
  340. print_info "Patch successfully applied!"
  341. patch_applied=true
  342. else
  343. print_warn "Failed to apply patch (dry-run passed but actual application failed)"
  344. fi
  345. else
  346. print_warn "Patch cannot be applied automatically (version mismatch)"
  347. # Check differences
  348. print_info "Checking patch differences..."
  349. if [[ -f "/opt/Eye/docs/patches/USM.pm" ]]; then
  350. diff -u "$USM_PM_FILE" "/opt/Eye/docs/patches/USM.pm" > /tmp/usm.diff 2>/dev/null || true
  351. if [[ -s /tmp/usm.diff ]]; then
  352. print_warn "Differences found in USM.pm file"
  353. echo "Differences:"
  354. head -20 /tmp/usm.diff
  355. echo "..."
  356. fi
  357. fi
  358. fi
  359. fi
  360. # If patch not applied, ask user
  361. if [[ "$patch_applied" == false ]]; then
  362. echo ""
  363. print_warn "Automatic patch application failed"
  364. print_warn "Modification of USM.pm file required for SNMPv3 with SHA512 support"
  365. echo ""
  366. read -p "Do you need SNMPv3 SHA512 support? (y/n): " -n 1 -r
  367. echo
  368. if [[ $REPLY =~ ^[Yy]$ ]]; then
  369. # Try to replace the entire file
  370. if [[ -f "/opt/Eye/docs/patches/USM.pm" ]]; then
  371. print_info "Replacing USM.pm file entirely..."
  372. # Check version compatibility
  373. local original_ver=$(grep -i "version" "$USM_PM_FILE" | head -1)
  374. local patch_ver=$(grep -i "version" "/opt/Eye/docs/patches/USM.pm" | head -1)
  375. if [[ -n "$original_ver" && -n "$patch_ver" ]]; then
  376. print_info "Original file version: $original_ver"
  377. print_info "Patch version: $patch_ver"
  378. fi
  379. # Create additional backup
  380. cp "$USM_PM_FILE" "${USM_PM_FILE}.backup.$(date +%Y%m%d_%H%M%S)"
  381. # Replace file
  382. cp -f "/opt/Eye/docs/patches/USM.pm" "$USM_PM_FILE"
  383. # Check if replacement successful
  384. if grep -q "SHA-512" "$USM_PM_FILE"; then
  385. print_info "USM.pm file successfully replaced, SHA512 support added"
  386. # Save replacement info
  387. echo "USM.pm file was replaced for SHA512 support" > "${USM_PM_FILE}.replaced"
  388. echo "Original file saved as: ${USM_PM_FILE}.backup" >> "${USM_PM_FILE}.replaced"
  389. echo "Replacement date: $(date)" >> "${USM_PM_FILE}.replaced"
  390. return 0
  391. else
  392. print_error "Failed to add SHA512 support after file replacement"
  393. # Restore from backup
  394. cp "${USM_PM_FILE}.backup" "$USM_PM_FILE"
  395. return 1
  396. fi
  397. else
  398. print_error "Patched USM.pm file not found in /opt/Eye/docs/patches/"
  399. return 1
  400. fi
  401. else
  402. print_info "SNMPv3 SHA512 support disabled"
  403. return 0
  404. fi
  405. fi
  406. return 0
  407. }
  408. # Download and copy source code
  409. install_source_code() {
  410. print_step "Installing Eye source code"
  411. # Create directory structure
  412. print_info "Creating directory structure..."
  413. mkdir -p /opt/Eye/scripts/cfg
  414. mkdir -p /opt/Eye/scripts/log
  415. mkdir -p /opt/Eye/html/cfg
  416. mkdir -p /opt/Eye/html/js
  417. mkdir -p /opt/Eye/docs
  418. chmod -R 755 /opt/Eye/html
  419. chmod -R 770 /opt/Eye/scripts/log
  420. chmod 750 /opt/Eye/scripts
  421. # Copy files
  422. print_info "Copying files..."
  423. cp -R scripts/ /opt/Eye/
  424. cp -R html/ /opt/Eye/
  425. cp -R docs/ /opt/Eye/
  426. # Set permissions
  427. chown -R eye:eye /opt/Eye
  428. # Apply SNMP SHA512 patch
  429. apply_snmp_patch
  430. }
  431. # Download additional scripts
  432. download_additional_scripts() {
  433. print_step "Downloading additional scripts"
  434. # Create directories
  435. mkdir -p /opt/Eye/html/js/jq
  436. mkdir -p /opt/Eye/html/js/select2
  437. mkdir -p /opt/Eye/html/js/jstree
  438. # Download jQuery
  439. print_info "Downloading jQuery..."
  440. if ! wget -q https://code.jquery.com/jquery-3.7.0.min.js \
  441. -O /opt/Eye/html/js/jq/jquery.min.js; then
  442. print_warn "Failed to download jQuery, trying alternative source..."
  443. wget -q https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js \
  444. -O /opt/Eye/html/js/jq/jquery.min.js || \
  445. print_error "Failed to download jQuery"
  446. fi
  447. # Download Select2
  448. print_info "Downloading Select2..."
  449. if wget -q https://github.com/select2/select2/archive/4.0.12.tar.gz -O 4.0.12.tar.gz; then
  450. tar -xzf 4.0.12.tar.gz -C /opt/Eye/html/js/select2/ \
  451. --strip-components=2 select2-4.0.12/dist 2>/dev/null || \
  452. tar -xzf 4.0.12.tar.gz -C /opt/Eye/html/js/select2/ \
  453. --strip-components=1 select2-4.0.12/dist 2>/dev/null
  454. rm -f 4.0.12.tar.gz
  455. else
  456. print_warn "Failed to download Select2"
  457. fi
  458. # Download jsTree
  459. print_info "Downloading jsTree..."
  460. if wget -q https://github.com/vakata/jstree/archive/3.3.12.tar.gz -O jstree.tar.gz; then
  461. tar -xzf jstree.tar.gz -C /opt/Eye/html/js/
  462. mv /opt/Eye/html/js/jstree-3.3.12/dist/* /opt/Eye/html/js/jstree
  463. rm -rf /opt/Eye/html/js/jstree-3.3.12
  464. rm -f jstree.tar.gz
  465. else
  466. print_warn "Failed to download jsTree"
  467. fi
  468. # Set permissions
  469. chown -R eye:eye /opt/Eye/html/js
  470. }
  471. # Configure MySQL
  472. setup_mysql() {
  473. print_step "Configuring MySQL"
  474. # Start and enable service
  475. $SERVICE_MANAGER enable mariadb 2>/dev/null || \
  476. $SERVICE_MANAGER enable mysql 2>/dev/null || true
  477. $SERVICE_MANAGER start mariadb 2>/dev/null || \
  478. $SERVICE_MANAGER start mysql 2>/dev/null || true
  479. # Check MySQL access
  480. if ! command -v mysql &> /dev/null; then
  481. print_error "MySQL client not installed"
  482. return 1
  483. fi
  484. MYSQL_OPT="-u root"
  485. # Check access without password
  486. if mysql -u root -e "SELECT 1;" 2>/dev/null; then
  487. print_info "MySQL accessible with empty password"
  488. echo ""
  489. print_warn "IMPORTANT: Need to set root password for MySQL!"
  490. print_warn "After installation run: mysql_secure_installation"
  491. echo ""
  492. else
  493. # Ask for password and create config file
  494. read -p "Enter MySQL root user password: " DB_ROOT_PASSWORD
  495. echo ""
  496. # Create temporary config file
  497. MYSQL_CNF_FILE="/tmp/mysql_root_eye.cnf"
  498. echo "[client]" > "$MYSQL_CNF_FILE"
  499. echo "user=root" >> "$MYSQL_CNF_FILE"
  500. echo "password=$DB_ROOT_PASSWORD" >> "$MYSQL_CNF_FILE"
  501. chmod 600 "$MYSQL_CNF_FILE"
  502. # Check connection
  503. if mysql --defaults-extra-file="$MYSQL_CNF_FILE" -e "SELECT 1;" &>/dev/null; then
  504. print_info "Successfully connected to MySQL"
  505. MYSQL_OPT="--defaults-extra-file=$MYSQL_CNF_FILE"
  506. else
  507. print_error "Incorrect MySQL root password"
  508. rm -f "$MYSQL_CNF_FILE"
  509. return 1
  510. fi
  511. fi
  512. read -p "Create database and user for Eye? (y/n): " -n 1 -r
  513. echo
  514. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  515. print_warn "Database creation skipped. Create manually:"
  516. print_warn " mysql -u root -p < ${SQL_CREATE_FILE}"
  517. print_warn " mysql -u root -p stat < ${SQL_DATA_FILE}"
  518. if [[ -f "$MYSQL_CNF_FILE" ]]; then
  519. rm -f "$MYSQL_CNF_FILE"
  520. fi
  521. return 0
  522. fi
  523. # Generate password for stat user
  524. DB_PASSWORD=$(pwgen 16 1)
  525. MYSQL_PASSWORD=$DB_PASSWORD
  526. print_info "Importing database structure..."
  527. # Import main SQL file
  528. mysql $MYSQL_OPT < ${SQL_CREATE_FILE}
  529. if [[ $? -ne 0 ]]; then
  530. print_error "Error importing create_db.sql"
  531. if [[ -f "$MYSQL_CNF_FILE" ]]; then
  532. rm -f "$MYSQL_CNF_FILE"
  533. fi
  534. return 1
  535. fi
  536. print_info "Database structure imported"
  537. # Import data
  538. print_info "Importing initial data..."
  539. mysql $MYSQL_OPT stat < ${SQL_DATA_FILE}
  540. if [[ $? -ne 0 ]]; then
  541. print_warn "Error importing data.sql (data may already exist)"
  542. else
  543. print_info "Initial data imported"
  544. fi
  545. # Create stat user
  546. print_info "Creating user 'stat'..."
  547. mysql $MYSQL_OPT <<EOF
  548. CREATE USER IF NOT EXISTS 'stat'@'localhost' IDENTIFIED BY '$DB_PASSWORD';
  549. GRANT ALL PRIVILEGES ON stat.* TO 'stat'@'localhost';
  550. FLUSH PRIVILEGES;
  551. EOF
  552. if [[ $? -ne 0 ]]; then
  553. print_error "Error creating user 'stat'"
  554. if [[ -f "$MYSQL_CNF_FILE" ]]; then
  555. rm -f "$MYSQL_CNF_FILE"
  556. fi
  557. return 1
  558. fi
  559. print_info "User 'stat' successfully created"
  560. # Save password information
  561. echo "MySQL 'stat' user password: $DB_PASSWORD" > /root/eye_mysql_password.txt
  562. chmod 600 /root/eye_mysql_password.txt
  563. print_info "User 'stat' password: $DB_PASSWORD"
  564. print_warn "Password saved in /root/eye_mysql_password.txt"
  565. # Clean up temporary file if created
  566. if [[ -f "$MYSQL_CNF_FILE" ]]; then
  567. rm -f "$MYSQL_CNF_FILE"
  568. fi
  569. return 0
  570. }
  571. # Configure PostgreSQL
  572. setup_postgresql() {
  573. print_step "Configuring PostgreSQL"
  574. # Start and enable service
  575. $SERVICE_MANAGER enable postgresql
  576. $SERVICE_MANAGER start postgresql
  577. # Check PostgreSQL access
  578. if ! command -v psql &> /dev/null; then
  579. print_error "PostgreSQL client not installed"
  580. return 1
  581. fi
  582. # Switch to postgres user to execute commands
  583. read -p "Create database and user for Eye? (y/n): " -n 1 -r
  584. echo
  585. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  586. print_warn "Database creation skipped. Create manually as postgres user:"
  587. print_warn " sudo -u postgres psql -f ${SQL_CREATE_FILE}"
  588. print_warn " sudo -u postgres psql -d stat -f ${SQL_DATA_FILE}"
  589. return 0
  590. fi
  591. # Generate password for stat user
  592. DB_PASSWORD=$(pwgen 16 1)
  593. POSTGRES_PASSWORD=$DB_PASSWORD
  594. print_info "Importing database structure..."
  595. # Import main SQL file as postgres user
  596. sudo -u postgres psql -f ${SQL_CREATE_FILE}
  597. if [[ $? -ne 0 ]]; then
  598. print_error "Error importing create_db.sql"
  599. return 1
  600. fi
  601. print_info "Database structure imported"
  602. # Set password for stat user
  603. print_info "Setting password for user 'stat'..."
  604. sudo -u postgres psql -c "ALTER USER stat WITH PASSWORD '$DB_PASSWORD';"
  605. # Import data
  606. print_info "Importing initial data..."
  607. sudo -u postgres psql -d stat -f ${SQL_DATA_FILE}
  608. if [[ $? -ne 0 ]]; then
  609. print_warn "Error importing data.sql (data may already exist)"
  610. else
  611. print_info "Initial data imported"
  612. fi
  613. # Configure PostgreSQL for MD5 authentication
  614. local pg_hba_file="/etc/postgresql/$(ls /etc/postgresql/ | head -1)/main/pg_hba.conf"
  615. if [[ -f "$pg_hba_file" ]]; then
  616. # Backup original
  617. cp "$pg_hba_file" "${pg_hba_file}.backup"
  618. # Add local md5 authentication if not present
  619. if ! grep -q "local.*stat.*md5" "$pg_hba_file"; then
  620. echo "local stat stat md5" >> "$pg_hba_file"
  621. print_info "Added MD5 authentication for stat user in pg_hba.conf"
  622. fi
  623. fi
  624. # Restart PostgreSQL to apply changes
  625. $SERVICE_MANAGER restart postgresql
  626. # Save password information
  627. echo "PostgreSQL 'stat' user password: $DB_PASSWORD" > /root/eye_postgres_password.txt
  628. chmod 600 /root/eye_postgres_password.txt
  629. print_info "User 'stat' password: $DB_PASSWORD"
  630. print_warn "Password saved in /root/eye_postgres_password.txt"
  631. return 0
  632. }
  633. # Configure database based on selected type
  634. setup_database() {
  635. # Выбор правильных SQL файлов для импорта данных
  636. if [[ "$DB_TYPE" == "mysql" ]]; then
  637. if [[ "$EYE_LANG" == "russian" && -d "/opt/Eye/docs/databases/mysql/ru" ]]; then
  638. SQL_DATA_FILE="/opt/Eye/docs/databases/mysql/ru/data.sql"
  639. SQL_CREATE_FILE="/opt/Eye/docs/databases/mysql/ru/create_db.sql"
  640. else
  641. SQL_DATA_FILE="/opt/Eye/docs/databases/mysql/en/data.sql"
  642. SQL_CREATE_FILE="/opt/Eye/docs/databases/mysql/en/create_db.sql"
  643. fi
  644. elif [[ "$DB_TYPE" == "postgresql" ]]; then
  645. if [[ "$EYE_LANG" == "russian" && -d "/opt/Eye/docs/databases/postgres/ru" ]]; then
  646. SQL_DATA_FILE="/opt/Eye/docs/databases/postgres/ru/data.sql"
  647. SQL_CREATE_FILE="/opt/Eye/docs/databases/postgres/ru/create_db.sql"
  648. else
  649. SQL_DATA_FILE="/opt/Eye/docs/databases/postgres/en/data.sql"
  650. SQL_CREATE_FILE="/opt/Eye/docs/databases/postgres/en/create_db.sql"
  651. fi
  652. fi
  653. print_info "Using SQL files for $EYE_LANG language"
  654. if [[ "$DB_TYPE" == "postgresql" ]]; then
  655. setup_postgresql
  656. else
  657. setup_mysql
  658. fi
  659. }
  660. # Configure configuration files
  661. setup_configs() {
  662. print_step "Configuring configuration files"
  663. # Copy configuration files
  664. if [[ -f "/opt/Eye/html/cfg/config.sample.php" ]]; then
  665. cp /opt/Eye/html/cfg/config.sample.php /opt/Eye/html/cfg/config.php
  666. fi
  667. if [[ -f "/opt/Eye/scripts/cfg/config.sample" ]]; then
  668. cp /opt/Eye/scripts/cfg/config.sample /opt/Eye/scripts/cfg/config
  669. fi
  670. # Generate encryption keys
  671. print_info "Generating encryption keys..."
  672. if command -v pwgen &> /dev/null; then
  673. ENC_PASSWORD=$(pwgen 16 1)
  674. else
  675. ENC_PASSWORD=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c16)
  676. fi
  677. ENC_VECTOR=$(tr -dc 0-9 </dev/urandom | head -c 16)
  678. # Configure config.php
  679. if [[ -f "/opt/Eye/html/cfg/config.sample.php" ]]; then
  680. cp /opt/Eye/html/cfg/config.sample.php /opt/Eye/html/cfg/config.php
  681. # Update database configuration based on type
  682. if [[ "$DB_TYPE" == "postgresql" ]]; then
  683. # PostgreSQL configuration
  684. if [[ -n "$POSTGRES_PASSWORD" ]]; then
  685. sed -i "s/define(\"DB_PASS\",\"[^\"]*\");/define(\"DB_PASS\",\"$POSTGRES_PASSWORD\");/" /opt/Eye/html/cfg/config.php
  686. fi
  687. sed -i "s/define(\"DB_TYPE\",\"[^\"]*\");/define(\"DB_TYPE\",\"postgresql\");/" /opt/Eye/html/cfg/config.php
  688. sed -i "s/define(\"DB_HOST\",\"[^\"]*\");/define(\"DB_HOST\",\"localhost\");/" /opt/Eye/html/cfg/config.php
  689. sed -i "s/define(\"DB_PORT\",\"[^\"]*\");/define(\"DB_PORT\",\"5432\");/" /opt/Eye/html/cfg/config.php
  690. sed -i "s/define(\"DB_NAME\",\"[^\"]*\");/define(\"DB_NAME\",\"stat\");/" /opt/Eye/html/cfg/config.php
  691. sed -i "s/define(\"DB_USER\",\"[^\"]*\");/define(\"DB_USER\",\"stat\");/" /opt/Eye/html/cfg/config.php
  692. else
  693. # MySQL configuration
  694. if [[ -n "$MYSQL_PASSWORD" ]]; then
  695. sed -i "s/define(\"DB_PASS\",\"[^\"]*\");/define(\"DB_PASS\",\"$MYSQL_PASSWORD\");/" /opt/Eye/html/cfg/config.php
  696. fi
  697. sed -i "s/define(\"DB_TYPE\",\"[^\"]*\");/define(\"DB_TYPE\",\"mysql\");/" /opt/Eye/html/cfg/config.php
  698. sed -i "s/define(\"DB_HOST\",\"[^\"]*\");/define(\"DB_HOST\",\"localhost\");/" /opt/Eye/html/cfg/config.php
  699. sed -i "s/define(\"DB_PORT\",\"[^\"]*\");/define(\"DB_PORT\",\"3306\");/" /opt/Eye/html/cfg/config.php
  700. sed -i "s/define(\"DB_NAME\",\"[^\"]*\");/define(\"DB_NAME\",\"stat\");/" /opt/Eye/html/cfg/config.php
  701. sed -i "s/define(\"DB_USER\",\"[^\"]*\");/define(\"DB_USER\",\"stat\");/" /opt/Eye/html/cfg/config.php
  702. fi
  703. # Update encryption key
  704. sed -i "s/ENCRYPTION_KEY\",\"[^\"]*\"/ENCRYPTION_KEY\",\"$ENC_PASSWORD\"/" /opt/Eye/html/cfg/config.php
  705. sed -i "s/ENCRYPTION_KEY','[^']*'/ENCRYPTION_KEY','$ENC_PASSWORD'/" /opt/Eye/html/cfg/config.php
  706. # Update initialization vector
  707. sed -i "s/ENCRYPTION_IV\",\"[^\"]*\"/ENCRYPTION_IV\",\"$ENC_VECTOR\"/" /opt/Eye/html/cfg/config.php
  708. sed -i "s/ENCRYPTION_IV','[^']*'/ENCRYPTION_IV','$ENC_VECTOR'/" /opt/Eye/html/cfg/config.php
  709. print_info "Configuration file config.php created from template"
  710. fi
  711. # Configure config for scripts
  712. if [[ -f "/opt/Eye/scripts/cfg/config.sample" ]]; then
  713. cp /opt/Eye/scripts/cfg/config.sample /opt/Eye/scripts/cfg/config
  714. # Update database configuration based on type
  715. if [[ "$DB_TYPE" == "postgresql" ]]; then
  716. # PostgreSQL configuration
  717. sed -i "s/^DBTYPE=.*/DBTYPE=postgresql/" /opt/Eye/scripts/cfg/config
  718. sed -i "s/DBTYPE=db_type/DBTYPE=postgresql/" /opt/Eye/scripts/cfg/config
  719. # Update database connection settings
  720. sed -i "s/^DBHOST=.*/DBHOST=localhost/" /opt/Eye/scripts/cfg/config
  721. sed -i "s/^DBPORT=.*/DBPORT=5432/" /opt/Eye/scripts/cfg/config
  722. if [[ -n "$POSTGRES_PASSWORD" ]]; then
  723. sed -i "s/^DBPASS=.*/DBPASS=$POSTGRES_PASSWORD/" /opt/Eye/scripts/cfg/config
  724. sed -i "s/DBPASS=db_password/DBPASS=$POSTGRES_PASSWORD/" /opt/Eye/scripts/cfg/config
  725. fi
  726. else
  727. # MySQL configuration
  728. sed -i "s/^DBTYPE=.*/DBTYPE=mysql/" /opt/Eye/scripts/cfg/config
  729. sed -i "s/DBTYPE=db_type/DBTYPE=mysql/" /opt/Eye/scripts/cfg/config
  730. # Update database connection settings
  731. sed -i "s/^DBHOST=.*/DBHOST=localhost/" /opt/Eye/scripts/cfg/config
  732. sed -i "s/^DBPORT=.*/DBPORT=3306/" /opt/Eye/scripts/cfg/config
  733. if [[ -n "$MYSQL_PASSWORD" ]]; then
  734. sed -i "s/^DBPASS=.*/DBPASS=$MYSQL_PASSWORD/" /opt/Eye/scripts/cfg/config
  735. sed -i "s/DBPASS=db_password/DBPASS=$MYSQL_PASSWORD/" /opt/Eye/scripts/cfg/config
  736. fi
  737. fi
  738. # Common settings
  739. sed -i "s/^DBNAME=.*/DBNAME=stat/" /opt/Eye/scripts/cfg/config
  740. sed -i "s/DBNAME=db_database/DBNAME=stat/" /opt/Eye/scripts/cfg/config
  741. sed -i "s/^DBUSER=.*/DBUSER=stat/" /opt/Eye/scripts/cfg/config
  742. sed -i "s/DBUSER=db_user/DBUSER=stat/" /opt/Eye/scripts/cfg/config
  743. # Update encryption key
  744. sed -i "s/^encryption_key=.*/encryption_key=$ENC_PASSWORD/" /opt/Eye/scripts/cfg/config
  745. sed -i "s/encryption_key=!!!CHANGE_ME!!!!/encryption_key=$ENC_PASSWORD/" /opt/Eye/scripts/cfg/config
  746. # Update initialization vector
  747. sed -i "s/^encryption_iv=.*/encryption_iv=$ENC_VECTOR/" /opt/Eye/scripts/cfg/config
  748. sed -i "s/encryption_iv=0123456789012345/encryption_iv=$ENC_VECTOR/" /opt/Eye/scripts/cfg/config
  749. print_info "Configuration file scripts/cfg/config created from template"
  750. fi
  751. # Set permissions
  752. chown -R eye:eye /opt/Eye/html/cfg /opt/Eye/scripts/cfg
  753. chmod 660 /opt/Eye/html/cfg/config.php /opt/Eye/scripts/cfg/config
  754. chmod 750 /opt/Eye/html/cfg /opt/Eye/scripts/cfg
  755. print_info "Encryption keys generated"
  756. print_info "Password: $ENC_PASSWORD"
  757. print_info "Vector: $ENC_VECTOR"
  758. }
  759. # Функция применения языковых настроек к конфигурации
  760. apply_language_settings() {
  761. print_info "Applying language settings: $EYE_LANG"
  762. # Настройка config.php
  763. if [[ -f "/opt/Eye/html/cfg/config.php" ]]; then
  764. if [[ "$EYE_LANG" == "russian" ]]; then
  765. # Установка русского языка
  766. sed -i "s/define(\"HTML_LANG\",\"english\");/define(\"HTML_LANG\",\"russian\");/g" /opt/Eye/html/cfg/config.php
  767. sed -i "s/setlocale(LC_ALL, 'en_US\.UTF-8');/setlocale(LC_ALL, 'ru_RU.UTF8');/g" /opt/Eye/html/cfg/config.php
  768. print_info "Web interface language set to Russian"
  769. else
  770. # Установка английского языка (по умолчанию)
  771. sed -i "s/define(\"HTML_LANG\",\"russian\");/define(\"HTML_LANG\",\"english\");/g" /opt/Eye/html/cfg/config.php
  772. sed -i "s/setlocale(LC_ALL, 'ru_RU\.UTF8');/setlocale(LC_ALL, 'en_US.UTF-8');/g" /opt/Eye/html/cfg/config.php
  773. print_info "Web interface language set to English"
  774. fi
  775. fi
  776. }
  777. # Configure Apache and PHP
  778. setup_apache_php() {
  779. print_step "Configuring Apache and PHP"
  780. # Determine PHP version
  781. PHP_VERSION=$(php -v 2>/dev/null | head -n1 | grep -oP '\d+\.\d+' || echo "8.1")
  782. # Configure PHP for all distributions
  783. if [[ "$OS_FAMILY" == "alt" ]]; then
  784. # ALT Linux
  785. PHP_INI="/etc/php/$PHP_VERSION/apache2/php.ini"
  786. APACHE_CONF_DIR="/etc/httpd2/conf"
  787. APACHE_SITES_DIR="$APACHE_CONF_DIR/sites-available"
  788. DEFAULT_CONF="$APACHE_SITES_DIR/000-default.conf"
  789. APACHE_USER="apache2"
  790. else
  791. # Debian/Ubuntu
  792. PHP_INI="/etc/php/$PHP_VERSION/apache2/php.ini"
  793. APACHE_CONF_DIR="/etc/apache2"
  794. APACHE_SITES_DIR="$APACHE_CONF_DIR/sites-available"
  795. DEFAULT_CONF="$APACHE_SITES_DIR/000-default.conf"
  796. APACHE_USER="www-data"
  797. fi
  798. # Configure Apache
  799. if [[ -f "/opt/Eye/docs/apache/000-default.conf" ]]; then
  800. print_info "Using prepared Apache template for ALT Linux"
  801. # Create directory if it doesn't exist
  802. mkdir -p "$APACHE_SITES_DIR"
  803. # Copy prepared config
  804. cp "/opt/Eye/docs/apache/000-default.conf" "$DEFAULT_CONF"
  805. # Enable site
  806. if [[ -f "$APACHE_CONF_DIR/sites-enabled/000-default.conf" ]]; then
  807. rm -f "$APACHE_CONF_DIR/sites-enabled/000-default.conf"
  808. ln -sf "$DEFAULT_CONF" "$APACHE_CONF_DIR/sites-enabled/000-default.conf"
  809. fi
  810. fi
  811. # Configure sudoers
  812. if [[ -f "/opt/Eye/docs/sudoers.d/www-data" ]]; then
  813. # Use prepared template, substituting correct user
  814. sed "s/www-data/eye/g" /opt/Eye/docs/sudoers.d/www-data > /etc/sudoers.d/eye
  815. chmod 440 /etc/sudoers.d/eye
  816. print_info "Sudoers file created from template"
  817. fi
  818. # Restart Apache
  819. if [[ "$OS_FAMILY" == "alt" ]]; then
  820. # ALT Linux uses httpd2
  821. APACHE_SERVICE="httpd2"
  822. else
  823. APACHE_SERVICE="apache2"
  824. fi
  825. # usermod -a -G eye $APACHE_USER
  826. if [[ "$OS_FAMILY" == "debian" ]]; then
  827. a2dismod php${PHP_VERSION} 2>/dev/null
  828. a2dismod mpm_prefork 2>/dev/null
  829. a2enmod mpm_event 2>/dev/null
  830. a2enconf php${PHP_VERSION}-fpm 2>/dev/null
  831. fi
  832. mkdir -p /var/log/php-fpm/
  833. a2enmod setenvif
  834. a2enmod proxy
  835. a2enmod proxy_fcgi
  836. print_info "Apache configured, sudoers user: $APACHE_USER"
  837. print_info "Apache service: $APACHE_SERVICE"
  838. # Configure php-fpm
  839. print_info "Configure php-fpm${PHP_VERSION}"
  840. if [[ -f "/opt/Eye/docs/php-fpm/eye.conf" ]]; then
  841. print_info "Using prepared php-fpm template"
  842. if [[ "$OS_FAMILY" == "alt" ]]; then
  843. cp "/opt/Eye/docs/php-fpm/eye.conf" /etc/fpm${PHP_VERSION}/php-fpm.d/
  844. else
  845. cp "/opt/Eye/docs/php-fpm/eye.conf" /etc/php/${PHP_VERSION}/fpm/pool.available/
  846. ln -sf "/etc/php/${PHP_VERSION}/fpm/pool.available/eye.conf" "/etc/php/${PHP_VERSION}/fpm/pool.d/eye.conf"
  847. fi
  848. fi
  849. $SERVICE_MANAGER enable "$APACHE_SERVICE"
  850. $SERVICE_MANAGER restart "$APACHE_SERVICE"
  851. $SERVICE_MANAGER enable php${PHP_VERSION}-fpm.service
  852. $SERVICE_MANAGER restart php${PHP_VERSION}-fpm.service
  853. # Check configuration
  854. if [[ "$OS_FAMILY" == "alt" ]]; then
  855. httpd2 -t 2>/dev/null && print_info "Apache (httpd2) configuration is valid" || print_warn "Check Apache configuration"
  856. else
  857. apache2ctl -t 2>/dev/null && print_info "Apache configuration is valid" || print_warn "Check Apache configuration"
  858. fi
  859. }
  860. # Configure cron and logrotate
  861. setup_cron_logrotate() {
  862. print_step "Configuring cron and logrotate"
  863. # Cron
  864. if [[ -f "/opt/Eye/docs/cron/stat" ]]; then
  865. cp /opt/Eye/docs/cron/stat /etc/cron.d/eye
  866. chmod 644 /etc/cron.d/eye
  867. print_info "Cron job added: /etc/cron.d/eye"
  868. fi
  869. # Logrotate
  870. if [[ -f "/opt/Eye/docs/logrotate/dnsmasq" ]]; then
  871. cp /opt/Eye/docs/logrotate/dnsmasq /etc/logrotate.d/dnsmasq-eye
  872. fi
  873. if [[ -f "/opt/Eye/docs/logrotate/scripts" ]]; then
  874. cp /opt/Eye/docs/logrotate/scripts /etc/logrotate.d/eye-scripts
  875. fi
  876. print_info "Cron and logrotate configuration completed"
  877. print_warn "Edit /etc/cron.d/eye to enable required scripts"
  878. }
  879. # Configure DHCP server (dnsmasq)
  880. setup_dhcp_server() {
  881. print_step "Configuring DHCP server"
  882. read -p "Configure DHCP server (dnsmasq)? (y/n): " -n 1 -r
  883. echo
  884. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  885. return 0
  886. fi
  887. # Backup configuration
  888. if [[ -f "/etc/dnsmasq.conf" ]]; then
  889. cp /etc/dnsmasq.conf /etc/dnsmasq.conf.backup
  890. fi
  891. # Copy configuration from Eye
  892. if [[ -f "/opt/Eye/docs/addons/dnsmasq.conf" ]]; then
  893. cat /opt/Eye/docs/addons/dnsmasq.conf > /etc/dnsmasq.conf
  894. fi
  895. # Copy systemd services
  896. if [[ -f "/opt/Eye/docs/systemd/dhcp-log.service" ]]; then
  897. cp /opt/Eye/docs/systemd/dhcp-log.service /etc/systemd/system/
  898. fi
  899. if [[ -f "/opt/Eye/docs/systemd/dhcp-log-truncate.service" ]]; then
  900. cp /opt/Eye/docs/systemd/dhcp-log-truncate.service /etc/systemd/system/
  901. fi
  902. # Enable services
  903. $SERVICE_MANAGER enable dnsmasq
  904. $SERVICE_MANAGER start dnsmasq
  905. print_info "DHCP server configured"
  906. print_warn "Edit /etc/dnsmasq.conf for your network"
  907. }
  908. # Configure syslog-ng
  909. setup_syslog() {
  910. print_step "Configuring syslog-ng"
  911. read -p "Configure remote log collection (syslog-ng)? (y/n): " -n 1 -r
  912. echo
  913. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  914. return 0
  915. fi
  916. # Create backup of main config
  917. if [[ -f "/etc/syslog-ng/syslog-ng.conf" ]]; then
  918. cp /etc/syslog-ng/syslog-ng.conf /etc/syslog-ng/syslog-ng.conf.backup
  919. print_info "Backup created: /etc/syslog-ng/syslog-ng.conf.backup"
  920. fi
  921. # Copy additional config for Eye
  922. if [[ -f "/opt/Eye/docs/syslog-ng/eye.conf" ]]; then
  923. mkdir -p /etc/syslog-ng/conf.d
  924. cp /opt/Eye/docs/syslog-ng/eye.conf /etc/syslog-ng/conf.d/eye.conf
  925. # Check if conf.d inclusion already exists in main config
  926. if [[ -f "/etc/syslog-ng/syslog-ng.conf" ]]; then
  927. if ! grep -q "@include.*conf\.d" /etc/syslog-ng/syslog-ng.conf && \
  928. ! grep -q "include.*conf\.d" /etc/syslog-ng/syslog-ng.conf; then
  929. # Add conf.d directory inclusion to end of file
  930. echo "" >> /etc/syslog-ng/syslog-ng.conf
  931. echo "# Include Eye monitoring configuration" >> /etc/syslog-ng/syslog-ng.conf
  932. echo "@include \"/etc/syslog-ng/conf.d/*.conf\"" >> /etc/syslog-ng/syslog-ng.conf
  933. print_info "Added conf.d directory inclusion to syslog-ng.conf"
  934. fi
  935. fi
  936. print_info "Configuration file eye.conf copied to /etc/syslog-ng/conf.d/"
  937. else
  938. print_warn "eye.conf configuration file not found in /opt/Eye/docs/syslog-ng/"
  939. fi
  940. # options block
  941. syslogng_options='options {
  942. chain_hostnames(off);
  943. flush_lines(0);
  944. use_dns(no);
  945. use_fqdn(no);
  946. dns_cache(no);
  947. owner("root");
  948. group("adm");
  949. perm(0640);
  950. stats_freq(0);
  951. time_reopen(10);
  952. log_fifo_size(1000);
  953. create_dirs(yes);
  954. keep_hostname(no);
  955. };'
  956. # Check for options in main config
  957. if [[ -f "/etc/syslog-ng/syslog-ng.conf" ]]; then
  958. if ! grep -q "^options\s*{" /etc/syslog-ng/syslog-ng.conf; then
  959. # Add options block if it doesn't exist
  960. if grep -q "^@version:" /etc/syslog-ng/syslog-ng.conf; then
  961. # Insert after @version: line
  962. sed -i "/^@version:/a\\$syslogng_options" /etc/syslog-ng/syslog-ng.conf
  963. else
  964. # Insert at beginning of file
  965. sed -i "1i\\$syslogng_options" /etc/syslog-ng/syslog-ng.conf
  966. fi
  967. print_info "Added options block to syslog-ng.conf"
  968. else
  969. # Check for required parameters in existing options block
  970. local missing_params=()
  971. if ! grep -q "time_reopen\s*(.*)" /etc/syslog-ng/syslog-ng.conf; then
  972. missing_params+=("time_reopen(10)")
  973. fi
  974. if ! grep -q "log_fifo_size\s*(.*)" /etc/syslog-ng/syslog-ng.conf; then
  975. missing_params+=("log_fifo_size(1000)")
  976. fi
  977. if ! grep -q "chain_hostnames\s*(.*)" /etc/syslog-ng/syslog-ng.conf; then
  978. missing_params+=("chain_hostnames(off)")
  979. fi
  980. if ! grep -q "create_dirs\s*(.*)" /etc/syslog-ng/syslog-ng.conf; then
  981. missing_params+=("create_dirs(yes)")
  982. fi
  983. if ! grep -q "keep_hostname\s*(.*)" /etc/syslog-ng/syslog-ng.conf; then
  984. missing_params+=("keep_hostname(no)")
  985. fi
  986. # Add missing parameters
  987. if [[ ${#missing_params[@]} -gt 0 ]]; then
  988. # Find options block and add parameters to end of block
  989. sed -i '/^options\s*{/,/^}/ {
  990. /^}/ i\ '"$(IFS='; '; echo "${missing_params[*]}")"';
  991. }' /etc/syslog-ng/syslog-ng.conf
  992. print_info "Added parameters to options block: ${missing_params[*]}"
  993. fi
  994. fi
  995. fi
  996. # Copy systemd service for Eye log processing
  997. if [[ -f "/opt/Eye/docs/systemd/syslog-stat.service" ]]; then
  998. cp /opt/Eye/docs/systemd/syslog-stat.service /etc/systemd/system/
  999. chmod 644 /etc/systemd/system/syslog-stat.service
  1000. print_info "syslog-stat service copied"
  1001. fi
  1002. # Create log directory if it doesn't exist
  1003. mkdir -p /opt/Eye/scripts/log
  1004. chown eye:eye /opt/Eye/scripts/log
  1005. chmod 770 /opt/Eye/scripts/log
  1006. # Enable and start services
  1007. $SERVICE_MANAGER daemon-reload
  1008. if $SERVICE_MANAGER enable syslog-ng; then
  1009. print_info "syslog-ng service enabled for autostart"
  1010. else
  1011. print_warn "Failed to enable syslog-ng for autostart"
  1012. fi
  1013. if $SERVICE_MANAGER restart syslog-ng; then
  1014. print_info "syslog-ng service restarted"
  1015. else
  1016. print_warn "Failed to restart syslog-ng"
  1017. fi
  1018. if [[ -f "/etc/systemd/system/syslog-stat.service" ]]; then
  1019. if $SERVICE_MANAGER enable syslog-stat; then
  1020. print_info "syslog-stat service enabled for autostart"
  1021. else
  1022. print_warn "Failed to enable syslog-stat for autostart"
  1023. fi
  1024. if $SERVICE_MANAGER start syslog-stat; then
  1025. print_info "syslog-stat service started"
  1026. else
  1027. print_warn "Failed to start syslog-stat"
  1028. fi
  1029. fi
  1030. # Check syslog-ng configuration
  1031. if command -v syslog-ng &> /dev/null; then
  1032. if syslog-ng --syntax-only; then
  1033. print_info "syslog-ng configuration is valid"
  1034. else
  1035. print_error "Error in syslog-ng configuration"
  1036. print_warn "Check files: /etc/syslog-ng/syslog-ng.conf and /etc/syslog-ng/conf.d/eye.conf"
  1037. fi
  1038. fi
  1039. print_info "syslog-ng configuration completed"
  1040. print_info "To receive logs from devices, configure them to send to IP: $(hostname -f)"
  1041. }
  1042. # Configure additional services
  1043. setup_additional_services() {
  1044. print_step "Configuring additional services"
  1045. # stat-sync service
  1046. if [[ -f "/opt/Eye/docs/systemd/stat-sync.service" ]]; then
  1047. cp /opt/Eye/docs/systemd/stat-sync.service /etc/systemd/system/
  1048. $SERVICE_MANAGER enable stat-sync.service
  1049. print_info "stat-sync service enabled"
  1050. fi
  1051. # eye-statd service (NetFlow)
  1052. if [[ -f "/opt/Eye/docs/systemd/eye-statd.service" ]]; then
  1053. cp /opt/Eye/docs/systemd/eye-statd.service /etc/systemd/system/
  1054. $SERVICE_MANAGER enable eye-statd.service
  1055. print_info "eye-statd service (NetFlow) enabled"
  1056. fi
  1057. # Configure DHCP
  1058. setup_dhcp_server
  1059. # Configure syslog
  1060. setup_syslog
  1061. }
  1062. # Import MAC address database
  1063. import_mac_database() {
  1064. print_step "Importing MAC address database"
  1065. if [[ -f "/opt/Eye/scripts/utils/mac-oids/download-macs.sh" ]]; then
  1066. cd /opt/Eye/scripts/utils/mac-oids/
  1067. # Download MAC database
  1068. print_info "Downloading MAC address database..."
  1069. bash download-macs.sh
  1070. # Update vendors
  1071. if [[ -f "update-mac-vendors.pl" ]]; then
  1072. print_info "Updating vendor information..."
  1073. perl update-mac-vendors.pl
  1074. fi
  1075. cd - >/dev/null
  1076. else
  1077. print_warn "MAC address import scripts not found"
  1078. fi
  1079. }
  1080. # Final instructions
  1081. show_final_instructions() {
  1082. echo ""
  1083. echo -e "${GREEN}===========================================${NC}"
  1084. echo -e "${GREEN} INSTALLATION COMPLETED SUCCESSFULLY! ${NC}"
  1085. echo -e "${GREEN}===========================================${NC}"
  1086. echo ""
  1087. echo "SYSTEM INFORMATION:"
  1088. echo " Distribution: $OS_NAME"
  1089. echo " Version: $OS_VERSION"
  1090. echo " Database: $DB_TYPE"
  1091. echo " Language: $EYE_LANG" # <-- Добавлено
  1092. echo " User: eye"
  1093. echo " Directory: /opt/Eye"
  1094. echo ""
  1095. echo ""
  1096. echo "TO COMPLETE SETUP, EXECUTE:"
  1097. echo ""
  1098. echo "1. Configure database security:"
  1099. if [[ "$DB_TYPE" == "postgresql" ]]; then
  1100. print_info " PostgreSQL: Edit pg_hba.conf if needed"
  1101. if [[ -f "/root/eye_postgres_password.txt" ]]; then
  1102. echo ""
  1103. echo "3. PostgreSQL 'stat' user password saved in:"
  1104. echo " /root/eye_postgres_password.txt"
  1105. echo ""
  1106. fi
  1107. else
  1108. echo " mysql_secure_installation"
  1109. if [[ -f "/root/eye_mysql_password.txt" ]]; then
  1110. echo ""
  1111. echo "3. MySQL 'stat' user password saved in:"
  1112. echo " /root/eye_mysql_password.txt"
  1113. echo ""
  1114. fi
  1115. fi
  1116. echo ""
  1117. echo "2. Check and edit configuration files:"
  1118. echo " /opt/Eye/html/cfg/config.php"
  1119. echo " /opt/Eye/scripts/cfg/config"
  1120. echo ""
  1121. echo "4. Configure cron jobs:"
  1122. echo " nano /etc/cron.d/eye"
  1123. echo " Uncomment required scripts"
  1124. echo ""
  1125. echo "5. Configure if necessary:"
  1126. echo " - DHCP: /etc/dnsmasq.conf"
  1127. echo " - NetFlow: configure on network devices"
  1128. echo ""
  1129. echo "6. WEB INTERFACE ACCESS:"
  1130. echo " URL: http://$(hostname -f)/"
  1131. echo " Admin: http://$(hostname -f)/admin/"
  1132. echo " Login: admin"
  1133. echo " Password: admin"
  1134. echo ""
  1135. echo -e "${RED}IMPORTANT:${NC}"
  1136. echo " - CHANGE admin password and API key!"
  1137. echo " - Configure users and networks in web interface"
  1138. echo ""
  1139. echo -e "${GREEN}===========================================${NC}"
  1140. echo ""
  1141. }
  1142. # Main function
  1143. main() {
  1144. clear
  1145. echo -e "${GREEN}===========================================${NC}"
  1146. echo -e "${GREEN} Installing Eye Monitoring System ${NC}"
  1147. echo -e "${GREEN} for ALT Linux/Debian/Ubuntu ${NC}"
  1148. echo -e "${GREEN}===========================================${NC}"
  1149. echo ""
  1150. # Глобальные переменные
  1151. MYSQL_PASSWORD=""
  1152. POSTGRES_PASSWORD=""
  1153. DB_TYPE="mysql"
  1154. EYE_LANG="english"
  1155. EYE_LANG_SHORT="en"
  1156. SQL_DATA_FILE=
  1157. SQL_CREATE_FILE=
  1158. # Execute installation steps
  1159. check_root
  1160. detect_distro
  1161. select_language_with_auto
  1162. select_database_type
  1163. update_system
  1164. install_packages
  1165. create_user_group
  1166. install_source_code
  1167. download_additional_scripts
  1168. setup_database
  1169. setup_configs
  1170. apply_language_settings
  1171. setup_apache_php
  1172. setup_cron_logrotate
  1173. setup_additional_services
  1174. import_mac_database
  1175. show_final_instructions
  1176. }
  1177. # Handle command line arguments
  1178. case "$1" in
  1179. --help|-h)
  1180. echo "Usage: $0 [options]"
  1181. echo ""
  1182. echo "Options:"
  1183. echo " --help, -h Show this help"
  1184. echo " --auto Automatic installation (minimal interaction)"
  1185. echo ""
  1186. echo "Supported distributions:"
  1187. echo " - ALT Linux 11.1+"
  1188. echo " - Debian 11+"
  1189. echo " - Ubuntu 20.04+"
  1190. echo ""
  1191. exit 0
  1192. ;;
  1193. --auto)
  1194. # Mode with minimal interaction
  1195. print_warn "Automatic mode. All confirmations will be accepted as 'yes'"
  1196. export DEBIAN_FRONTEND=noninteractive
  1197. ;;
  1198. *)
  1199. # Interactive mode by default
  1200. ;;
  1201. esac
  1202. # Start installation
  1203. main "$@"
  1204. # Exit with success code
  1205. exit 0