diff --git a/app/controllers/admin/sites_controller.rb b/app/controllers/admin/sites_controller.rb index e9e581a9c..0ac656698 100644 --- a/app/controllers/admin/sites_controller.rb +++ b/app/controllers/admin/sites_controller.rb @@ -14,6 +14,13 @@ class Admin::SitesController < OrbitBackendController # @site = Site.new # end + def show_system_preference + @git_commit_list_file = File.new(OrbitSystemPreference::GitCommitListPath, "r") rescue nil + @db_backup_list_file = File.new(OrbitSystemPreference::ArchiveDbListPath, "r") rescue nil + @resque_logs_file = File.new(OrbitSystemPreference::ResqueLogFile, "r") rescue nil + @site = Site.first + end + def update @site.update_attributes(params[:site]) site_restart diff --git a/app/jobs/backup_server.rb b/app/jobs/backup_server.rb index 3f0ae6b71..ae7128b10 100644 --- a/app/jobs/backup_server.rb +++ b/app/jobs/backup_server.rb @@ -5,9 +5,13 @@ class BackupServer #CronMail.time_check("Going to backup Orbit").deliver dbhost = Mongoid.config.database.connection.primary.join ':' dbname = Mongoid.config.database.name + archive_db_list_path = OrbitSystemPreference::ArchiveDbListPath dbdirectory = "#{Rails.root}/tmp/#{dbname}-"+Time.now.strftime("%Y-%m-%d-%H-%M") %x[mongodump -h #{dbhost} -d #{dbname} -o #{dbdirectory} ] - # %x[touch #{Rails.root}/tmp/restart] + %x[rm #{archive_db_list_path}] + %x[ls #{Rails.root}/tmp/#{dbname}* | du -h --max-depth=1 --block-size=1M |sort -h >> #{archive_db_list_path}] + OrbitJobLogger.info "DB backup done Path:#{dbdirectory}" + end end diff --git a/app/jobs/dashboard_counter.rb b/app/jobs/dashboard_counter.rb new file mode 100644 index 000000000..ce934aaee --- /dev/null +++ b/app/jobs/dashboard_counter.rb @@ -0,0 +1,36 @@ +class DashboardCounter < Resque::Plugins::RestrictionJob + @queue = :high + + def self.perform(*args) + site = Site.first + obj = new(*args) + site.dashboard_counter[:visitors_this_week] = obj.display_visitors_this_week + site.dashboard_counter[:visitors_this_month] = obj.display_visitors_this_month + site.dashboard_counter[:visitors_this_year] = obj.display_visitors_this_year + site.save + OrbitJobLogger.info "DashboardCounter done #{site.dashboard_counter.to_s}" + end + + + def display_visitors(options={}) + impressions = Impression.where(options).and(:referrer.ne => nil) + impressions.map{|i| i[:session_hash]}.uniq.count + end + + def display_visitors_today + display_visitors(created_at: {'$gte' => Date.today.beginning_of_day, '$lte' => Date.today.end_of_day}) + end + + def display_visitors_this_week + display_visitors(created_at: {'$gte' => Date.today.beginning_of_week, '$lte' => Date.today.end_of_week}) + end + + def display_visitors_this_month + display_visitors(created_at: {'$gte' => Date.today.beginning_of_month, '$lte' => Date.today.end_of_month}) + end + + def display_visitors_this_year + display_visitors(created_at: {'$gte' => Date.today.beginning_of_year, '$lte' => Date.today.end_of_year}) + end + +end diff --git a/app/jobs/generate_system_summary.rb b/app/jobs/generate_system_summary.rb new file mode 100644 index 000000000..782c18ae7 --- /dev/null +++ b/app/jobs/generate_system_summary.rb @@ -0,0 +1,46 @@ +class GenerateSystemSummary + @queue = :high + + def self.perform() + @site = Site.first + get_disk_free + get_git_log_list + get_package_info + @site.save + end + + def self.get_package_info + @info = {} + get_nginx_version + get_MongoDB_version + get_Linux_version + @site.system_package_info = @info + end + + def self.get_git_log_list + git_commit_list_path = OrbitSystemPreference::GitCommitListPath + %x[rm #{git_commit_list_path}] + %x[cd #{Rails.root};#{OrbitSystemPreference::GitLogCommend} >>#{git_commit_list_path} ] + end + + def self.get_disk_free + @site.disk_space= %x[#{OrbitSystemPreference::DiskFree}] + end + + def self.get_nginx_version + @info[:nginx] = %x[#{OrbitSystemPreference::SystemPackage::NginxVersion} ] + end + + def self.get_MongoDB_version + @info[:mongodb] = %x[#{OrbitSystemPreference::SystemPackage::MongodbVersion} ] + end + + def self.get_Linux_version + @info[:linux] = %x[#{OrbitSystemPreference::SystemPackage::SystemVersion} ] + end + + def self.check_system_has_enough_space( limit =OrbitSystemPreference::DefaultDiskSpaceLimit ) + binding.pry + end + +end \ No newline at end of file diff --git a/app/jobs/nccu_calendar.rb b/app/jobs/nccu_calendar.rb deleted file mode 100644 index df60bdd25..000000000 --- a/app/jobs/nccu_calendar.rb +++ /dev/null @@ -1,21 +0,0 @@ -class NccuCalendar - require 'open-uri' - require 'tempfile' - - @queue = :high - - def self.perform() - # temp_file = Tempfile.new('new_cal') - # open('http://events.nccu.edu.tw/Month').read{|data| - # temp_file << data - # } - -open(File.join(Rails.root, 'public/static', 'nccu_calendar.xml'), 'wb') do |fo| - fo.print open('http://events.nccu.edu.tw/Month').read -end - - # FileUtils.mv(temp_file, File.join(Rails.root, 'public/static', 'nccu_calendar.xml')) - - puts "[#{ DateTime.now.strftime("%Y %D %H:%M")}]NccuCalendar Synced #{File.join(Rails.root, 'public/static', 'nccu_calendar.xml')}" - end -end diff --git a/app/jobs/sync_db.rb b/app/jobs/sync_db.rb deleted file mode 100644 index 9c92e816d..000000000 --- a/app/jobs/sync_db.rb +++ /dev/null @@ -1,25 +0,0 @@ -class SyncDb - - @queue = :high - - def self.perform() - puts "[ #{DateTime.now.strftime("%Y %D %H:%M") }]\t SyncDb Starting" - self.start_sync - self.set_admin - end - - def self.start_sync - task = 'mid_site:sync' - args = [] - %x[rake #{task} --trace >> #{Rails.root}/log/rake.log] - User.all.each{|ur| ur.create_dept_cache} - puts "[#{ DateTime.now.strftime("%Y %D %H:%M")}]\tSyncDb Synced" - end - - def self.set_admin - task = 'mid_site:install_admin' - args = [] - %x[rake #{task} --trace >> #{Rails.root}/log/rake.log] - puts "[#{ DateTime.now.strftime("%Y %D %H:%M")}]\tAdmin done" - end -end diff --git a/app/jobs/update_tag_cloud.rb b/app/jobs/update_tag_cloud.rb index e0df88d4b..08fd16ee0 100644 --- a/app/jobs/update_tag_cloud.rb +++ b/app/jobs/update_tag_cloud.rb @@ -5,5 +5,6 @@ class UpdateTagCloud Tag.all.each do |tag| tag.update_attribute(:cloud_view_count, tag.impressionist_count(:created_at.gte => 14.days.ago, :created_at.lte => Time.now)) end + OrbitJobLogger.info "UpdateTagCloud Done" end end \ No newline at end of file diff --git a/app/models/site.rb b/app/models/site.rb index c4df3fc30..07facaa79 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -18,7 +18,10 @@ class Site field :title_always_on, :type => Boolean, :default => false field :dashbroad_allow_visitor, :type => Boolean, :default => false field :mail_settings, :type => Hash - + + field :disk_space, :type => String + field :system_package_info, :type => Hash,:default=>{} + field :school field :department diff --git a/app/views/admin/sites/show_system_preference.html.erb b/app/views/admin/sites/show_system_preference.html.erb new file mode 100644 index 000000000..19360afb6 --- /dev/null +++ b/app/views/admin/sites/show_system_preference.html.erb @@ -0,0 +1,47 @@ +

<%= I18n.t("site.system_preference") %>

+ + +
+
+

<%= I18n.t("site.system_preference_.summary.disk_space") %>:

+ <%= content_tag :p,@site.disk_space %> +

<%= I18n.t("site.system_preference_.summary.code_update_at") %>:

+ <% @site.system_package_info.each do |index,value| %> +

<%= index.titleize %> <%= I18n.t("site.system_preference_.summary.version") %>:

+ <%= content_tag :p,value %> + <% end %> +

<%= I18n.t("site.system_preference_.summary.weekness_report") %>:<%= '' %>

+
+
+ <% if @git_commit_list_file %> + <% @git_commit_list_file.lines do |line|%> + <%= (line + "
").html_safe %> + <% end%> + <% else %> +
<%= I18n.t("site.system_preference_.summary.no_data") %>
+ <% end %> +
+
+ <%if @db_backup_list_file %> + <% @db_backup_list_file.lines do |line|%> + <%=(line + "
").html_safe %> + <% end %> + <% else %> +
<%= I18n.t("site.system_preference_.summary.no_data") %>
+ <% end %> +
+
+ <%if @db_backup_list_file %> + <% @resque_logs_file.lines do |line|%> + <%=(line + "
").html_safe %> + <% end if @resque_logs_file%> + <%else %> +
<%= I18n.t("site.system_preference_.summary.no_data") %>
+ <% end %> +
+
\ No newline at end of file diff --git a/config/environment.rb b/config/environment.rb index bdc566b53..0d9f5fdb9 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -5,4 +5,5 @@ YAML::ENGINE.yamler = 'syck' # Initialize the rails application Orbit::Application.initialize! -Me = Site.first \ No newline at end of file +Me = Site.first +OrbitJobLogger = OrbitJobLog.new \ No newline at end of file diff --git a/config/initializers/orbit_system_preference.rb b/config/initializers/orbit_system_preference.rb new file mode 100644 index 000000000..2f7e8cb6a --- /dev/null +++ b/config/initializers/orbit_system_preference.rb @@ -0,0 +1,13 @@ +module OrbitSystemPreference + ArchiveDbListPath = "#{Rails.root}/log/archive_db.list.log" + GitCommitListPath = "#{Rails.root}/log/git_commit.list.log" + ResqueLogFile ="#{Rails.root}/log/orbit_job.log" + GitLogCommend = 'git log --pretty=format:"%x09%ad%x09%s" --date=short' + DiskFree= 'df -h /' + DefaultDiskSpaceLimit = 3 #in GB + module SystemPackage + MongodbVersion = "mongod --version" + NginxVersion = "nginx -V" + SystemVersion = "uname -a" + end +end \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 102ef2314..a2604c57c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -389,6 +389,18 @@ en: search_help: Please Enter the search argument for Google search. settings: Site setting sub_menu: Site sub-menu + system_preference: System Preference + system_preference_: + tab_backups: Backups + tab_commits: Commits + tab_summary: Summary + tab_logs: Logs + summary: + code_update_at: Code Update histroy + disk_space: Disk Free + no_data: No Data + version: Version + weekness_report: Weekness Report title: Site Title title_help: Site Title Guide site_: Site diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 94e8d43d6..73db5dbc3 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -389,6 +389,18 @@ zh_tw: search_help: 請輸入送交Google搜尋的參數 settings: 基本設定 sub_menu: 次選單 + system_preference: 系統狀態 + system_preference_: + tab_backups: 備份記錄 + tab_commits: 程式版本 + tab_summary: 總覽 + tab_logs: 登錄檔 + summary: + code_update_at: 程式更新紀錄 + disk_space: 硬碟空間 + no_data: 沒有資訊 + version: 版本 + weekness_report: 弱點掃瞄資訊 title: 網站標題 title_help: 網站標題說明 site_: 網站 diff --git a/config/resque.god b/config/resque.god index afd706c01..8a1b03ab9 100644 --- a/config/resque.god +++ b/config/resque.god @@ -1,14 +1,9 @@ -#developer pls change here -rails_root = "/home/nccu/stage/NCCU" #keep this blank when development +rails_env = ENV['RAILS_ENV'] || "production" +rails_root = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/..' +user_home = ENV['HOME'] || File.dirname(__FILE__) + '/../..' +development_uid = '' +development_gid = '' -development_uid = 'kaito' #when dev -development_gid = 'staff' #when dev - -#rails_env = "developement" -rails_env = "production" - -development_rails_root = File.expand_path("..",File.dirname(__FILE__)) -#rails_root = (rails_env == 'production' )? production_rails_root : development_rails_root num_workers = rails_env == 'production' ? 5 : 2 num_workers.times do |num| @@ -16,16 +11,26 @@ num_workers.times do |num| w.dir = rails_root w.name = "resque-#{num}" - w.group = 'resque-stage' + w.group = 'nccu_production' w.interval = 30.seconds - w.env = {"QUEUE"=>"critical,high,low", "RAILS_ENV"=>rails_env} - w.start = "rake -f #{rails_root}/Rakefile resque:work QUEUE=* RAILS_ENV=#{rails_env}" + + queue = case num + when 0 + 'critical' + when 1..2 + 'high,low' + when 3..num_workers + 'low' + end + + w.env = {"QUEUE"=>queue, "RAILS_ENV"=>rails_env} + w.start = "HOME=#{user_home} rake -f #{rails_root}/Rakefile resque:work QUEUE=* RAILS_ENV=#{rails_env}" w.uid = (rails_env == 'production' )? "root" : development_uid w.gid = (rails_env == 'production' )? "root" : development_gid - w.log = (rails_env == 'production' )? "/var/log/#{w.name}-stage.log":"#{rails_root}/log/#{w.name}.log" + w.log = (rails_env == 'production' )? "/var/log/#{w.group}-#{w.name}":"#{rails_root}/log/dev_resque-#{w.name}.log" # restart if memory gets too high w.transition(:up, :restart) do |on| diff --git a/config/resque_schedule.god b/config/resque_schedule.god index 34a6dff8c..5f136438a 100644 --- a/config/resque_schedule.god +++ b/config/resque_schedule.god @@ -1,27 +1,21 @@ -#developer pls change here -rails_root = "/home/nccu/NCCU/" #keep this blank when development - -development_uid = 'kaito' #when dev -development_gid = 'staff' #when dev - -#rails_env = "developement" -rails_env = "production" - -development_rails_root = File.expand_path("..",File.dirname(__FILE__)) -#rails_root = (rails_env == 'production' )? production_rails_root : development_rails_root +rails_env = ENV['RAILS_ENV'] || "production" +rails_root = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/..' +user_home = ENV['HOME'] || File.dirname(__FILE__) + '/../..' +development_uid = '' +development_gid = '' God.watch do |w| w.dir = rails_root w.name = "resque-scheduler" - w.group = 'resque' + w.group = 'nccu_production' w.interval = 30.seconds w.env = {"QUEUE"=>"critical,high,low", "RAILS_ENV"=>rails_env} - w.start = "rake -f #{rails_root}/Rakefile resque:scheduler RAILS_ENV=#{rails_env}" + w.start = "HOME= #{user_home} rake -f #{rails_root}/Rakefile resque:scheduler RAILS_ENV=#{rails_env}" w.uid = (rails_env == 'production' )? "root" : development_uid w.gid = (rails_env == 'production' )? "root" : development_gid - w.log = (rails_env == 'production' )? "/var/log/#{w.name}.log":"#{rails_root}/log/#{w.name}.log" + w.log = (rails_env == 'production' )? "/var/log/#{w.group}-#{w.name}.log":"#{rails_root}/log/dev_resque-#{w.name}.log" # restart if memory gets too high w.transition(:up, :restart) do |on| diff --git a/config/resque_schedule.yml b/config/resque_schedule.yml index 0fc798eb1..e73f96427 100644 --- a/config/resque_schedule.yml +++ b/config/resque_schedule.yml @@ -1,11 +1,11 @@ -# do_mail_matt: -# every: 10s -# class: FetchTime -# args: -# description: Runs the perform method in FetchTime - update_tag_cloud: cron: 0 0 [0,12] * * * class: UpdateTagCloud args: description: UpdateTagCloud + +generate_system_summary: + cron: 0 0 12 * * * + class: GenerateSystemSummary + args: + description: Generate the system status such as disk free space,package version list for showing at site tab \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 2e2dc8a55..b66121057 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,6 +17,7 @@ Orbit::Application.routes.draw do # routes for admin namespace :admin do + match 'system_preference' => "sites#show_system_preference",:as=>"system_preference" mount Resque::Server.new, :at => "/resque" resources :assets do collection do diff --git a/lib/NewBlog.zip b/lib/NewBlog.zip deleted file mode 100644 index 71f0e0910..000000000 Binary files a/lib/NewBlog.zip and /dev/null differ diff --git a/lib/orbit_job_log.rb b/lib/orbit_job_log.rb new file mode 100644 index 000000000..f8ba54888 --- /dev/null +++ b/lib/orbit_job_log.rb @@ -0,0 +1,32 @@ +class OrbitJobLog < Logger + FORMAT = "%m/%d/%Y %H:%M%p: " + def initialize + case Rails.env + when 'production' + # Logger::Syslog.new("orbit_routine", Syslog::LOG_LOCAL5) + super(Orbit::Application.config.root.to_s+'/log/orbit_job.log','daily') + when 'development' + super(Orbit::Application.config.root.to_s+'/log/orbit_job.dev.log','daily') + end + end + + def debug(msg) + super(Time.now.strftime()+msg) + end + + def info(msg) + super(Time.now.strftime(FORMAT)+msg) + end + + def warn(msg) + super(Time.now.strftime(FORMAT)+msg) + end + + def error(msg) + super(Time.now.strftime(FORMAT)+msg) + end + + def fatal(msg) + super(Time.now.strftime(FORMAT)+msg) + end +end \ No newline at end of file